8

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.

4 Answers 4

9

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.

Sign up to request clarification or add additional context in comments.

10 Comments

something like $(#treeview).live("click", function(){ var o = $(this); o.children('div').toggle(); o.filter('.parent').toggleClass('expanded'); BindGridView($(this).attr('id')); return false; });
@mike: What about the addClass? It doesn't have an impact, the selectors inside the function will work fine (looking for the parent class, I assume). The live call should work, but it's a little hard to tell from the comments. =)
@mike you can't dynamically add the class, but you can do something like this $(data.d).addClass('parent').appendTo('#treeview')
@Yads, it seems your addClass changes all my div's ids to treeview, which messes things up
@casperOne, your live suggestion seem to have worked but until I can dynamically addClass the whole thing doesn't work
|
4

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().

Comments

2

You can use delegate() or live() to bind event handlers to dynamic elements. In most cases, delegate() will be the most efficient route, unless you cannot predict where the dynamic elements will exist in the DOM.

Comments

1

Creating dynamic clickable objects had me foxed for some time too. I couldn’t get the delegate() function to work either. Eventually I found this example http://www.rahulsingla.com/blog/2011/03/jqueryui-adding-removing-buttons-dynamically-from-a-jqueryui-dialog

and modified it to

var buttonClear = $('#formResult').parent().find('#formMessage'); buttonClear.append("<input type='button' value='Clear message' id='clear'>");

$("#clear").click(function() { // do button click action here });

The formResult is a form id already on the page and the formMessage is a dynamically added message displayed after the form is submitted. The script adds a button to the end of the form to clear the message.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.