Tom Holder wrote:
Hi All,
I have some xhtml in my page with images that have certain events. A
click and a mouseover.
When a user uploads another image, it replaces the current ones using
AJAX.
My question is, how can I rebind whatever events where previously on
the images back on the new content?
I want to do something like (very basic example):
var clickFunction = $("img").click(function(){
alert('yo');
return false;
});
Replace image with AJAX.
$("img").click(clickFunction);
This doesn't seem to work though. The reason why I can't just rebind
the previous code is I don't actually know what the events will be as
they change according to other conditions.
I hope I've explained this ok.
Thanks
Tom
What I'm doing at the moment in this situation is putting all my event
bindings in a function (or functions) which i then reference in the ajax
callback parameter, so:
function clickFunction() {
$("img").click(function(){
alert('yo');
return false;
});
}
clickFunction();
$.ajax({
... stuff ...
}, clickFunction);
Replace image with AJAX and reapply events.
There's a page in the docs that has a bit more detail:
http://docs.jquery.com/Tutorials:AJAX_and_Events