Dynamically add jQuery event handlers to dynamic HTML
<p>I have a div section that I'm dynamically populating via jQuery ajax:</p>

<pre><code>$('#treeview').append(data.d);
</code></pre>

<p>Where data is a bunch of nested divs with different ids.</p>

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

<pre><code> $('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;
    });
</code></pre>

<p>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 &amp; dynamic data population doesn't work; however I can see my correct div layout on my page.</p>

<p>I'm guessing that I need to add the click event &amp; addClass when I'm doing the </p>

<p>$('#treeview').append(data.d);</p>

<p>But I can't figure out how.</p>
