Hi Karl,
This is as if you were reading my mind, this gold dude
Thanks heaps
--Kush
Karl Swedberg wrote:
Hi Kim,
My friend Jonathan Chaffer posted a reply to a similar question that I
think you might find helpful. Here it is:
In the near future there will be a dead-tree reference for jQuery on
the shelves. A short excerpt from the first draft should prove
applicable to this conversation:
-----------------------------------
AJAX and Events: Handling the Handlers
Suppose we wanted to highlight all the <h3> elements on the page when
they are clicked. By now the code to perform such a task is almost
second-nature:
$(document).ready(function() {
$('h3').click(function() {
$(this).toggleClass('highlighted');
});
});
All is well, in that clicking on the letters on the left side of the
page highlights them. But the dictionary terms are also <h3>
elements, and they do not get the highlight. Why?
The dictionary terms are not yet part of the DOM when the page is
loaded, so the event handlers are never bound. This is an example of
a general issue with event handlers and AJAX calls: loaded elements
must have all of their event handlers rebound.
A first pass at solving this problem is to factor the binding out
into a function, and call that function both at the time the document
is ready and after the AJAX call:
$(document).ready(function() {
var bindBehaviors = function() {
$('h3').click(function() {
$(this).toggleClass('highlighted');
});
}
bindBehaviors();
$('#letter-a .button').click(function() {
$('#dictionary').hide().load('a.html', function() {
bindBehaviors();
$(this).fadeIn();
});
});
});
Now we can put all our event handlers in the bindBehaviors()
function, and call that whenever the DOM changes. Clicking on a
dictionary term now highlights it, as we intended. Unfortunately,
we've also managed to cause very strange behavior when the letters
are clicked. At first they highlight correctly, but after the button
is clicked (loading the dictionary entries), they no longer highlight
on subsequent clicks.
Closer inspection reveals that, after the AJAX call, the highlighting
breaks because the click handler is fired twice. A
doubled .toggleClass() is the same as none at all, so the click seems
not to work. A tricky behavior to debug, to be sure. The culprit here
is bindBehaviors(), which binds the click event to all <h3> elements
each time. After a button click, there are actually two event
handlers for clicks on an <h3>, which happen to do the exact same thing.
Scoping an Event Binding Function
A nice way around this double-firing is to pass some context into
bindBehaviors() each time we call it. the $() constructor can take a
second argument, a DOM node to which the search is restricted. By
using this feature in bindBehaviors(), we can avoid multiple event
bindings:
$(document).ready(function() {
var bindBehaviors = function(scope) {
$('h3', scope).click(function() {
$(this).toggleClass('highlighted');
});
}
bindBehaviors(this);
$('#letter-a .button').click(function() {
$('#dictionary').hide().load('a.html', function() {
bindBehaviors(this);
$(this).fadeIn();
});
});
});
The first time bindBehaviors() is called, the scope is document, so
all <h3> elements in the document are matched and have the click
event bound. After an AJAX load, though, the scope is instead the
<div id="dictionary"> element, so the letters are not matched and are
left alone.
Using Event Bubbling
Adding scope to a behavior-binding function is often a very elegant
solution to the problem of binding event handlers after an AJAX load.
We can often avoid the issue entirely, however, by exploiting event
bubbling. We can bind the handler not to the elements that are
loaded, but to a common ancestor element:
$(document).ready(function() {
$('body').click(function(e) {
if ($(e.target).is('h3')) {
$(e.target).toggleClass('highlighted');
}
});
});
Here we bind the click event handler to the <body>element. Because
this is not in the portion of the document that is changed when the
AJAX call is made, the event handler never has to be re-bound.
However, the event context is now wrong, so we compensate for this by
checking what the event's target attribute is. If the target is of
the right type, we perform our normal action; otherwise, we do nothing.
--Karl
_________________
Karl Swedberg
www.englishrules.com
www.learningjquery.com
On Mar 18, 2007, at 2:24 PM, Kim Johnson wrote:
I have a div which is invisible by default, and
becomes visible via slidetoggle when a button is
clicked. Inside the div is dynamic content via ajax --
i take the responseText of an ajax call, and then
append it to an empty div (inside the slidetoggled
div) via .html(ajaxreponsevar). I do it this way
because I use the same div for multiple bits of
dynamic content, a different bit depending on which
button is clicked.
This approach works fine for getting/showing the
dynamic content. However, now I'm trying to add a
click event to an image inside that dynamically
generated content, and it's not working. I am thinking
this is maybe because my jquery bind events and such
are loaded once on page load (in a php file), and and
thus can't bind to something that doesn't exist yet?
I also tried ("divstuff img").bind(click, function(){
...etc (which I think is the same thing as just saying
divstuff img.click()) but that didn't work either.
I'm guessing there's some sort of trick to binding
events to dynamic content?
thanks!
-kim
____________________________________________________________________________________
It's here! Your new message!
Get new email alerts with the free Yahoo! Toolbar.
http://tools.search.yahoo.com/toolbar/features/mail/
_______________________________________________
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/
------------------------------------------------------------------------
_______________________________________________
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/
_______________________________________________
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/