By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,527 Members | 1,184 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,527 IT Pros & Developers. It's quick & easy.

Does removing an element also remove its event listeners?

P: n/a
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.
Nov 21 '08 #1
Share this Question
Share on Google+
6 Replies


P: n/a


try posting to comp.lang.javascript
On Fri, 21 Nov 2008 05:51:26 -0800 (PST), bgold12 <bg*****@gmail.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.
Nov 21 '08 #2

P: n/a
On Nov 21, 8:12*am, richard <mem...@newsguy.comwrote:
try posting to comp.lang.javascript

On Fri, 21 Nov 2008 05:51:26 -0800 (PST), bgold12 <bgol...@gmail.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.
Nov 21 '08 #3

P: n/a
On 2008-11-21, Stor Ursa <st******@gmail.comwrote:
On Nov 21, 8:12*am, richard <mem...@newsguy.comwrote:
>try posting to comp.lang.javascript

On Fri, 21 Nov 2008 05:51:26 -0800 (PST), bgold12 <bgol...@gmail.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.
Nov 21 '08 #4

P: n/a
bgold12 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?
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.
Nov 21 '08 #5

P: n/a
bgold12 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.
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.
Nov 21 '08 #6

P: n/a
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>
-----------------------
Nov 22 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.