Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically add jQuery event handlers to dynamic HTML

Tags:

jquery

I have a div section that I'm dynamically populating via jQuery ajax:

$('#treeview').append(data.d);

Where data is a bunch of nested divs with different ids.

I also have some jQuery code that makes the divs into a treeview, with a +/- expand/collapse and dynamic data population:

 $('div.tree div:has(div)').addClass('parent'); // Requires jQuery 1.2!
    $('div.tree div').click(function() {
        var o = $(this);
        o.children('div').toggle();
        o.filter('.parent').toggleClass('expanded');
        BindGridView($(this).attr('id'));
        return false;
    });

The issue is when I paste the divs into the main treeview div all is well. When I dynamically create the exact same text, yes I've compared it, the expand/collapse & dynamic data population doesn't work; however I can see my correct div layout on my page.

I'm guessing that I need to add the click event & addClass when I'm doing the

$('#treeview').append(data.d);

But I can't figure out how.

like image 712
mike Avatar asked Jan 24 '11 19:01

mike


People also ask

How do you add an event handler to a dynamic button?

To attach event handlers to the dynamically created button, we need to select the button with a class of btn and add an event listener of click .

Does jQuery work on dynamic content?

With jQuery, what happens is it bind event listeners on each DOM element inline and not on classes or ids. So when the binding method is called, events are bonded on the DOM loaded. To bind event on dynamically loaded DOM, we bind the event to the nearest static parent and give the element we want to bind event.

How to dynamically create HTML element in JavaScript?

With document. createElement() method you can create a specified HTML element dynamically in JavaScript. Once created, you can insert (or add) the element to your web page, or add it to a pre-defined element or a dynamically created element. In fact, you can create an entire form dynamically using this method.

Which method is used to bind a callback method for an event for DOM elements added at runtime?

You can use the live() method to bind elements (even newly created ones) to events and handlers, like the onclick event. Save this answer.


2 Answers

If you are dynamically adding elements to the DOM, then existing event handlers bound to a selector will not work (such as click).

You need to use the live function in order to have events from newly created DOM elements captured.


It should be noted (as it was in the comments by Zach L) that as of jQuery 1.7 live has been deprecated in favor of on. The general advice is the same (tracking dynamic elements), just the mechanism (on vs live) has changed.

like image 168
casperOne Avatar answered Oct 20 '22 20:10

casperOne


live() is deprecated.

From the docs:

Use .on() to attach event handlers. Users of older versions of jQuery should use .delegate() in preference to .live().

like image 33
Zach Lysobey Avatar answered Oct 20 '22 21:10

Zach Lysobey