473,385 Members | 1,642 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Does removing an element also remove its event listeners?

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
6 6015


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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Chien Lau | last post by:
I have a form class that acts as a top level window. The user can open and close any number of these windows throughout the lifetime of the application. In the OnLoad, I have: ...
1
by: mathon | last post by:
Hello, could anybody explain me the advantages and disadvantages of delegation and the advantages and disadvantages using event-listeners? - both related to the treatment of events. thanks in...
4
by: Deena | last post by:
Hi How do I create event listeners? I would like to listen for the "GotFocus" event for each control on a form. Deena
2
by: Csaba Gabor | last post by:
One of the things that I never understood was the motivation for not being able to iterate over the event listeners added via ..addEventListener It's particularly vexing because if you do...
8
by: moondaddy | last post by:
I'm working in WPF and c# and am adding an event handler to an object like this: this.tgt.SizeChanged += new SizeChangedEventHandler(OnTargetSizeChanged); Later I kill the instance of...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.