Attaching JQuery events to DOM elements introduced after page load
Posted by jamie on 16 Jun 2011 in Notes
If you load an element on your page after page load (i.e. via AJAX), basic JQuery event listeners such as this:
$(".my-selector").click(function(event) {
...
});
won't work, because that event listener only listens to elements that appear upon completion of page load. To catch elements introduced later via AJAX, use the live handler:
$(".my-selector").live('click', function(event) {
...
});
That will attach the 'click' event handler to all selected elements, even ones inserted into the DOM after page load. See the JQuery docs for more info: http://api.jquery.com/live/