Does removing an element also remove its event listeners? 
November 21st, 2008, 01:55 PM
| | | |
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. | 
November 21st, 2008, 02:15 PM
| | | | re: Does removing an element also remove its event listeners?
try posting to comp.lang.javascript
On Fri, 21 Nov 2008 05:51:26 -0800 (PST), bgold12 <bgold12@gmail.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.
| | 
November 21st, 2008, 03:35 PM
| | | | re: Does removing an element also remove its event listeners?
On Nov 21, 8:12*am, richard <mem...@newsguy.comwrote: Quote:
try posting to comp.lang.javascript
>
On Fri, 21 Nov 2008 05:51:26 -0800 (PST), bgold12 <bgol...@gmail.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:
| > Quote:
// get the element by its id
elem = document.getElementById('elemId');
| > Quote:
// attach the event listener
if (elem.addEventListener) { // std
* *elem.addEventListener(eventName, functionRef, false );
} else if (elem.attachEvent) { // ie
* *elem.attachEvent(eventName, functionRef );
} // end if
| > Quote:
... // whatever functionality makes use of the element and its event
(s)
| > Quote:
// delete the element
elem.parentNode.removeChild(elem);
| > Quote:
In this case, does the browser automatically destroy the events I
added previously, or do have to remove them myself?
| > | 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. | 
November 21st, 2008, 03:55 PM
| | | | re: Does removing an element also remove its event listeners?
On 2008-11-21, Stor Ursa <storursa@gmail.comwrote: Quote:
On Nov 21, 8:12*am, richard <mem...@newsguy.comwrote: Quote:
>try posting to comp.lang.javascript
>>
>On Fri, 21 Nov 2008 05:51:26 -0800 (PST), bgold12 <bgol...@gmail.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:
| >> Quote:
>// get the element by its id
>elem = document.getElementById('elemId');
| >> Quote:
>// attach the event listener
>if (elem.addEventListener) { // std
* *elem.addEventListener(eventName, functionRef, false );
>} else if (elem.attachEvent) { // ie
* *elem.attachEvent(eventName, functionRef );
>} // end if
| >> Quote:
>... // whatever functionality makes use of the element and its event
>(s)
| >> Quote:
>// delete the element
>elem.parentNode.removeChild(elem);
| >> Quote:
>In this case, does the browser automatically destroy the events I
>added previously, or do have to remove them myself?
| >> | >
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. | 
November 21st, 2008, 04:05 PM
| | | | re: Does removing an element also remove its event listeners?
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. | 
November 21st, 2008, 07:45 PM
| | | | re: Does removing an element also remove its event listeners?
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. | 
November 22nd, 2008, 12:05 AM
| | | | re: Does removing an element also remove its event listeners?
Webkit will kept it.
--------CODE-----------
<html>
<head>
<title>Handler Test</title>
<script type="text/javascript">
function addListener(node, event, funktion) {
if(node) {
if(document.addEventListener) {
node.addEventListener(event, function(e){funktion(e);}, false);
} else if(document.attachEvent) {
node.attachEvent("on"+event, function(){funktion();});
}
}
}
//global vars
var tButton = null;
function removeButton_Click(e) {
var t = document.getElementById("testButton");
tButton = t.parentNode.removeChild(t);
document.getElementById("addButton").style.display = "inline";
document.getElementById("removeButton").style.disp lay = "none";
}
function addButton_Click(e) {
document.body.appendChild(tButton);
document.getElementById("addButton").style.display = "none";
document.getElementById("removeButton").style.disp lay = "inline";
}
function testButton_Click(e) {
alert("You clicked the testButton.")
}
addListener(window, "load", function(){
addListener(document.getElementById("testButton"), 'click',
testButton_Click);
addListener(document.getElementById("addButton"), 'click',
addButton_Click);
addListener(document.getElementById("removeButton" ), 'click',
removeButton_Click);
});
</script>
</head>
<body>
<button id="removeButton">Remove Button</button<button
id="addButton" style="display:none">Add Button</button<br />
<button id="testButton">Test Button</button>
</body>
</html>
----------------------- |  |
Similar Threads | | Thread | Thread Starter | Forum | Replies | Last Post | | "modern" javascript... | maya | answers | 19 | July 20th, 2008 01:15 PM | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 225,662 network members.
|