473,765 Members | 1,957 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.getEle mentById('elemI d');

// attach the event listener
if (elem.addEventL istener) { // std
elem.addEventLi stener(eventNam e, functionRef, false );
} else if (elem.attachEve nt) { // ie
elem.attachEven t(eventName, functionRef );
} // end if

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

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

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 6060


try posting to comp.lang.javas cript
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.getEle mentById('elemI d');

// attach the event listener
if (elem.addEventL istener) { // std
elem.addEventLi stener(eventNam e, functionRef, false );
} else if (elem.attachEve nt) { // ie
elem.attachEven t(eventName, functionRef );
} // end if

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

// delete the element
elem.parentNod e.removeChild(e lem);

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.javas cript

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.getEle mentById('elemI d');
// attach the event listener
if (elem.addEventL istener) { // std
* *elem.addEventL istener(eventNa me, functionRef, false );
} else if (elem.attachEve nt) { // ie
* *elem.attachEve nt(eventName, functionRef );
} // end if
... // whatever functionality makes use of the element and its event
(s)
// delete the element
elem.parentNode .removeChild(el em);
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.javas cript

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.getEle mentById('elemI d');
>// attach the event listener
if (elem.addEventL istener) { // std
* *elem.addEventL istener(eventNa me, functionRef, false );
} else if (elem.attachEve nt) { // ie
* *elem.attachEve nt(eventName, functionRef );
} // end if
>... // whatever functionality makes use of the element and its event
(s)
>// delete the element
elem.parentNod e.removeChild(e lem);
>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(el em);

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.getEle mentById('elemI d');

// attach the event listener
if (elem.addEventL istener) { // std
elem.addEventLi stener(eventNam e, functionRef, false );
} else if (elem.attachEve nt) { // ie
elem.attachEven t(eventName, functionRef );
} // end if

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

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

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.getEle mentById('elemI d');

// attach the event listener
if (elem.addEventL istener) { // std
elem.addEventLi stener(eventNam e, functionRef, false );
} else if (elem.attachEve nt) { // ie
elem.attachEven t(eventName, functionRef );
} // end if

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

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

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>Handle r Test</title>
<script type="text/javascript">
function addListener(nod e, event, funktion) {
if(node) {
if(document.add EventListener) {
node.addEventLi stener(event, function(e){fun ktion(e);}, false);
} else if(document.att achEvent) {
node.attachEven t("on"+event, function(){funk tion();});
}
}
}

//global vars
var tButton = null;
function removeButton_Cl ick(e) {
var t = document.getEle mentById("testB utton");
tButton = t.parentNode.re moveChild(t);
document.getEle mentById("addBu tton").style.di splay = "inline";
document.getEle mentById("remov eButton").style .display = "none";
}

function addButton_Click (e) {
document.body.a ppendChild(tBut ton);
document.getEle mentById("addBu tton").style.di splay = "none";
document.getEle mentById("remov eButton").style .display = "inline";
}

function testButton_Clic k(e) {
alert("You clicked the testButton.")
}
addListener(win dow, "load", function(){
addListener(doc ument.getElemen tById("testButt on"), 'click',
testButton_Clic k);
addListener(doc ument.getElemen tById("addButto n"), 'click',
addButton_Click );
addListener(doc ument.getElemen tById("removeBu tton"), 'click',
removeButton_Cl ick);
});

</script>
</head>
<body>
<button id="removeButto n">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
394
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: Application.Idle+=new EventHandler(Application_Idle); ....and in the Dispose() override, I have: Application.Idle-=new EventHandler(Application_Idle);
1
1426
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 advance regards
4
1735
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
4601
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 element.cloneNode(bDeep), you frequently get a stunted clone, often with several key features missing, and one of these is associated event handlers. Of course, it could be the case that the event handlers have a closure including a reference to the...
8
9048
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 tgt like this:
0
9399
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10161
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9955
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9833
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7378
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5275
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3924
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3531
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.