jQuery
New Wave Javascript


discuss » Binding events to newly added DOM elements


Posted: Wed Mar 8 12:47:21 EST 2006
From: Michael Geary <Mike at Geary.com >

That would work, but I believe it would result in a memory leak in IE:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/IETechCol/d
nwebgen/ie_leak_patterns.asp

See the "DOM Insertion Order Leak Model" near the end of the article.

> From: John Resig
>
> So, long story short - I imagine that binding the event to the element
> *before* you inject it would work:
>
> function bindTest() {
> var el = document.createElement("input");
> $(el).set({value: "Test Button", type: "button"})
> .click(function() {
> alert("button clicked");
> });
> $("#bindTest").after(el);
> }