HighDots Forums  

Does removing an element also remove its event listeners?

alt.html alt.html


Discuss Does removing an element also remove its event listeners? in the alt.html forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
bgold12
 
Posts: n/a

Default Does removing an element also remove its event listeners? - 11-21-2008 , 08:51 AM






Hey, I just want to make sure that when I remove an element I don't
have to worry about the events listeners I added previously to the
element. For example:

// get the element by its id
elem = document.getElementById('elemId');

// attach the event listener
if (elem.addEventListener) { // std
elem.addEventListener(eventName, functionRef, false );
} else if (elem.attachEvent) { // ie
elem.attachEvent(eventName, functionRef );
} // end if

.... // whatever functionality makes use of the element and its event
(s)

// delete the element
elem.parentNode.removeChild(elem);

In this case, does the browser automatically destroy the events I
added previously, or do have to remove them myself?

Thanks.

Reply With Quote
  #2  
Old   
richard
 
Posts: n/a

Default Re: Does removing an element also remove its event listeners? - 11-21-2008 , 09:12 AM








try posting to comp.lang.javascript


On Fri, 21 Nov 2008 05:51:26 -0800 (PST), bgold12 <bgold12 (AT) gmail (DOT) com>
wrote:

Quote:
Hey, I just want to make sure that when I remove an element I don't
have to worry about the events listeners I added previously to the
element. For example:

// get the element by its id
elem = document.getElementById('elemId');

// attach the event listener
if (elem.addEventListener) { // std
elem.addEventListener(eventName, functionRef, false );
} else if (elem.attachEvent) { // ie
elem.attachEvent(eventName, functionRef );
} // end if

... // whatever functionality makes use of the element and its event
(s)

// delete the element
elem.parentNode.removeChild(elem);

In this case, does the browser automatically destroy the events I
added previously, or do have to remove them myself?

Thanks.

Reply With Quote
  #3  
Old   
Stor Ursa
 
Posts: n/a

Default Re: Does removing an element also remove its event listeners? - 11-21-2008 , 10:34 AM



On Nov 21, 8:12*am, richard <mem... (AT) newsguy (DOT) com> wrote:
Quote:
try posting to comp.lang.javascript

On Fri, 21 Nov 2008 05:51:26 -0800 (PST), bgold12 <bgol... (AT) gmail (DOT) com
wrote:

Hey, I just want to make sure that when I remove an element I don't
have to worry about the events listeners I added previously to the
element. For example:

// get the element by its id
elem = document.getElementById('elemId');

// attach the event listener
if (elem.addEventListener) { // std
* *elem.addEventListener(eventName, functionRef, false );
} else if (elem.attachEvent) { // ie
* *elem.attachEvent(eventName, functionRef );
} // end if

... // whatever functionality makes use of the element and its event
(s)

// delete the element
elem.parentNode.removeChild(elem);

In this case, does the browser automatically destroy the events I
added previously, or do have to remove them myself?

Thanks.
That's an interesting question. You could add the Element back to the
DOM after removing it and then see if it's still there when you click
on it.


Reply With Quote
  #4  
Old   
Ben C
 
Posts: n/a

Default Re: Does removing an element also remove its event listeners? - 11-21-2008 , 10:50 AM



On 2008-11-21, Stor Ursa <storursa (AT) gmail (DOT) com> wrote:
Quote:
On Nov 21, 8:12*am, richard <mem... (AT) newsguy (DOT) com> wrote:
try posting to comp.lang.javascript

On Fri, 21 Nov 2008 05:51:26 -0800 (PST), bgold12 <bgol... (AT) gmail (DOT) com
wrote:

Hey, I just want to make sure that when I remove an element I don't
have to worry about the events listeners I added previously to the
element. For example:

// get the element by its id
elem = document.getElementById('elemId');

// attach the event listener
if (elem.addEventListener) { // std
* *elem.addEventListener(eventName, functionRef, false );
} else if (elem.attachEvent) { // ie
* *elem.attachEvent(eventName, functionRef );
} // end if

... // whatever functionality makes use of the element and its event
(s)

// delete the element
elem.parentNode.removeChild(elem);

In this case, does the browser automatically destroy the events I
added previously, or do have to remove them myself?

Thanks.

That's an interesting question. You could add the Element back to the
DOM after removing it and then see if it's still there when you click
on it.
I'm fairly sure it should be. What I would expect to happen is for the
event listener to be garbage collected some time after all references to
the removed element have gone.

So if you write:

var oldChild = elem.parentNode.removeChild(elem);

nothing will be actually deleted until oldChild goes out of scope, by
which time of course it will be impossible to reattach the element.


Reply With Quote
  #5  
Old   
Stevo
 
Posts: n/a

Default Re: Does removing an element also remove its event listeners? - 11-21-2008 , 10:53 AM



bgold12 wrote:
Quote:
Hey, I just want to make sure that when I remove an element I don't
have to worry about the events listeners I added previously to the
element. For example:

// get the element by its id
elem = document.getElementById('elemId');

// attach the event listener
if (elem.addEventListener) { // std
elem.addEventListener(eventName, functionRef, false );
} else if (elem.attachEvent) { // ie
elem.attachEvent(eventName, functionRef );
} // end if

... // whatever functionality makes use of the element and its event
(s)

// delete the element
elem.parentNode.removeChild(elem);

In this case, does the browser automatically destroy the events I
added previously, or do have to remove them myself?
I'd trust a reply from someone like Thomas pointyears more than mine on
this, but it seems to me like you've got nothing to worry about here.

You haven't "added events", all you did was add a handler to be called
if that element generates those events. Now that the element is removed,
it can't generate any events. It is an interesting idea though to add
the element back in and see if your handler is still hooked in, although
I doubt this is something you planned to do.


Reply With Quote
  #6  
Old   
Daniel Orner
 
Posts: n/a

Default Re: Does removing an element also remove its event listeners? - 11-21-2008 , 02:42 PM



bgold12 wrote:
Quote:
Hey, I just want to make sure that when I remove an element I don't
have to worry about the events listeners I added previously to the
element. For example:

// get the element by its id
elem = document.getElementById('elemId');

// attach the event listener
if (elem.addEventListener) { // std
elem.addEventListener(eventName, functionRef, false );
} else if (elem.attachEvent) { // ie
elem.attachEvent(eventName, functionRef );
} // end if

... // whatever functionality makes use of the element and its event
(s)

// delete the element
elem.parentNode.removeChild(elem);

In this case, does the browser automatically destroy the events I
added previously, or do have to remove them myself?

Thanks.
It depends on your browser. IE6 in particular is known for being awful
with garbage collecting. The problem is that there is an implicit
circular closure between the event handler (which is a JavaScript
object) and the DOM tag itself (which is a DOM object and hence subject
to entirely different garbage collection routines). So the answer, if
you're worried about memory in IE6 (and possibly IE7, not sure) is no.
There has been a patch released which purportedly fixes this problem,
but I haven't had a chance to test it myself.


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.