473,394 Members | 1,721 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,394 software developers and data experts.

Removing an anonymous event listener

Is there a way to remove an anonymous event handler that I attached
using attachEventListener? Or to say it another way, is there a way to
remove all event handlers that have been attached to an element?

Consider the html / script below. After attaching the onClick handler
to the div, I'd like to remove it. Perhaps there is a way to query the
div for all its handlers?

Thanks,
Csaba Gabor from Vienna

<div id=testdiv style="border:1px solid blue">Click me</div>
<script type='text/javascript'>
var div=document.getElementById('testdiv');
div.addEventListener("click",
function() {alert("I've been clicked")}, false);
div.removeEventListener("click", div.onClick, false);
</script>

Aug 5 '05 #1
3 14833
You can also attach events like this:

div.onclick = function() { ... }

and remove them like this:

div.onclick = null;

Aug 5 '05 #2
On 05/08/2005 22:30, Csaba Gabor wrote:
Is there a way to remove an anonymous event handler that I attached
using attachEventListener?
If you didn't maintain a reference to the function object, then no. You
must be able to a pass a reference to the removal method in order for
the method to find and remove that listener.
Or to say it another way, is there a way to remove all event handlers
that have been attached to an element?
No.

[snip]
Perhaps there is a way to query the div for all its handlers?


No, there isn't.

[snip]

Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Aug 6 '05 #3


Csaba Gabor wrote:
Is there a way to remove an anonymous event handler that I attached
using attachEventListener? Or to say it another way, is there a way to
remove all event handlers that have been attached to an element? var div=document.getElementById('testdiv');
div.addEventListener("click",
function() {alert("I've been clicked")}, false);


No, that way with DOM Level 2 at least there is no way to remove that
event handler, you would need to store the function somewhere to be able
to pass it later to removeEventListener.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Aug 6 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Jeff Thies | last post by:
I have this IE specific bit of code for finding the originating node: var obj=window.event.srcElement; How do I do that cross browser (Opera, NS, Safari...)? Is there a standard DOM method? ...
2
by: Craig | last post by:
Hi I listen on a port, when data is received I raise an event (OnMessageReceived) in the while loop as follows: private void WaitForConnection() { TcpListener listener = new...
6
by: Steve Teeples | last post by:
I have been perplexed by how to best treat an event that spans different classes. For example, I have a form which a user inputs data. I want to broadcast that data via an event to another...
3
by: sowencheung | last post by:
I attach an Event in document.onkeyup and another event in document.onbeforeunload in onkeyup event, if the condition is satisfied, i will activate sth, then i don't want the onbeforeunload...
1
by: CB | last post by:
When designing an object, is there a best practice for how to allow the user of the object to tell the object to remove itself from any delegates / events it has registered with? For example.....
2
by: darthghandi | last post by:
I am trying to pass a socket object when an event is signaled. I have successfully bound to a network interface and listened on a port for incoming connection. I have also been able to accept...
6
by: blaine | last post by:
Hello, I'm currently overriding function keys (F1 to F4) to perform other actions. In order to do this the default popup windows of Help (F1), Seach(F3) etc must be turned off. In FF it's easy...
6
by: bgold12 | last post by:
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 =...
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: 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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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...

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.