Saturday, May 11, 2024
 Popular · Latest · Hot · Upcoming
185
rated 0 times [  188] [ 3]  / answers: 1 / hits: 17466  / 16 Years ago, tue, february 17, 2009, 12:00:00

What happens in jQuery when you remove() an element and append() it elsewhere?


It appears that the events are unhooked - as if you were just inserting fresh html (which I guess is what happening). But its also possible my code has a bug in it - so I just wanted to verify this behavior before I continue.


If this is the case - are there any easy ways to rehookup the events to just that portion of HTML, or a different way to move the element without losing the event in the first place.


More From » jquery

 Answers
16

Yes, jQuery's approach with remove() is to unbind everything bound with jQuery's own bind (to prevent memory leaks).



However, if you just want to move something in the DOM, you don't have to remove() it first. Just append to your heart's content, the event bindings will stick around :)



For example, paste this into your firebug on this page:



$('li.wmd-button:eq(2)').click(function(){ alert('still here!') }).appendTo(document.body)


And now scroll down to the bottom of this page and click on the little globy icon now buried under the SO footer. You will get the alert. All because I took care to not remove it first.


[#99960] Tuesday, February 10, 2009, 16 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
destiney

Total Points: 175
Total Questions: 96
Total Answers: 93

Location: Zambia
Member since Sat, Nov 6, 2021
3 Years ago
;