Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

How do I attach events to dynamic HTML elements with jQuery?

Assume I have some jQuery code that connects an event handler to all elements with class
.myclass.

For example:*
$(function(){
$(".myclass").click( function() {
// do something
});
});


What's more, my HTML may be as per the following:
<a class="myclass" href="#">test1</a>
<a class="myclass" href="#">test2</a>
<a class="myclass" href="#">test3</a>


That works with no issue. Be that as it may, consider if the .myclass elements were written to the page at some future time.

*For example:


<a id="anchor1" href="#">create link dynamically</a>
<script type="text/javascript">
$(function(){
$("#anchor1").click( function() {
$("#anchor1").append('<a class="myclass" href="#">test4</a>');
});
});
</script>


Essentially, I might want to write the click() overseer once and have it apply to both content present at page load, and content acquired later by means of AJAX/DHTML. Any thought how I can fix this?
by

3 Answers

espadacoder11
Use the jQuery functions .live() and .die(). Available in jQuery 1.3.x

From the docs:

To display each paragraph's text in an alert box whenever it is clicked:

$("p").live("click", function(){
alert( $(this).text() );
});
pankajshivnani123
You can bind a single click event to a page for all elements, no matter if they are already on that page or if they will arrive at some future time, like that:

$(document).bind('click', function (e) {
var target = $(e.target);
if (target.is('.myclass')) {
e.preventDefault(); // if you want to cancel the event flow
// do something
} else if (target.is('.myotherclass')) {
e.preventDefault();
// do something else
}
});

Been using it for a while. Works like a charm.

In jQuery 1.7 and later, it is recommended to use .on() in place of bind or any other event delegation method, but .bind() still works.
sandhya6gczb
The .live() method is deprecated as of jQuery 1.7.



As of jQuery 1.7, the .live() method is deprecated. Use .on() to attach event handlers. Users of older versions of jQuery should use .delegate() in preference to .live().

For jQuery 1.7+ you can attach an event handler to a parent element using .on(), and pass the a selector combined with 'myclass' as an argument.



So instead of...

$(".myclass").click( function() {
// do something
});

You can write...

$('body').on('click', 'a.myclass', function() {
// do something
});

This will work for all a tags with 'myclass' in the body, whether already present or dynamically added later.

The body tag is used here as the example had no closer static surrounding tag, but any parent tag that exists when the .on method call occurs will work. For instance a ul tag for a list which will have dynamic elements added would look like this:

$('ul').on('click', 'li', function() {
alert( $(this).text() );
});

As long as the ul tag exists this will work (no li elements need to exist yet).

Login / Signup to Answer the Question.