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

How to remove an event listener

Claus Mygind
571 512MB
I am using FireFox only

I am attaching a eventListner to a <span id="someId"> </span> tag


I am trying to add and remove eventListners on the fly.

I have no problem adding the listner, which I do like this:

Expand|Select|Wrap|Line Numbers
  1.  
  2. this.chkStat = function (lbl, elm, newValue)
  3. {
  4.   if (newValue == elm.value)
  5.   { 
  6.         //no listner attached if the values are the same
  7.     lbl.className = "myLowlight";
  8.   }else{
  9.         //listner attached if values not the same
  10.     lbl.className = "myHighlight";
  11.  
  12.     if (lbl.addEventListener)
  13.     {
  14.           lbl.addEventListener("mousedown",
  15.             function(evt)
  16.             {
  17.                showRealValue(this, elm.id, newValue);
  18.             },
  19.             false)
  20.     };
  21.  
  22.   }
  23. }
  24.  
When the user clicks on the label <span>, they may choose to update the adjacent field with the new value. At this point I want to remove the listner

I tried the following:
Expand|Select|Wrap|Line Numbers
  1.     lbl.removeEventListener('click', showRealValue, false);
  2.  
It did not remove the listner. Also when I displayed the next record. the old listner with the old values stayed active. Plus the next record's new value was added.

I basically just need to figure out how to remove the eventListner I added on the fly.

I tried to follow the example give at https://developer.mozilla.org/en-US/...eEventListener

with no success.

This may not be enough info, so if you think you can help but need further clarification just say so.
Jul 15 '13 #1

✓ answered by Rabbit

A few things.

1) The event you added is mousedown but the event you're trying to remove is click. They are not the same thing.

2) The function you added is not called showRealValue. That is merely a function that you call in the function that the event listener uses. If you followed the example, you would have assigned your unnamed function to a variable and then used the variable to add/remove the function.

3 3179
Rabbit
12,516 Expert Mod 8TB
A few things.

1) The event you added is mousedown but the event you're trying to remove is click. They are not the same thing.

2) The function you added is not called showRealValue. That is merely a function that you call in the function that the event listener uses. If you followed the example, you would have assigned your unnamed function to a variable and then used the variable to add/remove the function.
Jul 15 '13 #2
Claus Mygind
571 512MB
First item was just a copy and paste error, I do use the same event. I was just playing with both types.

Second item. Thanks for the clarification. This was just what I needed to solve the problem. I did not make that connection with the anonymous function assignment.
Jul 17 '13 #3
Sherin
77 64KB
Try This Code

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <style>
  4. #myDIV {
  5.   background-color: coral;
  6.   border: 1px solid;
  7.   padding: 50px;
  8.   color: white;
  9. }
  10. </style>
  11. </head>
  12. <body>
  13.  
  14. <div id="myDIV">
  15.   <p>Click the button to remove the DIV's event handler.</p>
  16.   <button onclick="removeHandler()" id="myBtn">Try it</button>
  17. </div>
  18.  
  19. <p id="demo"></p>
  20.  
  21. <script>
  22. document.getElementById("myDIV").addEventListener("mousemove", myFunction);
  23.  
  24. function myFunction() {
  25.   document.getElementById("demo").innerHTML = Math.random();
  26. }
  27.  
  28. function removeHandler() {
  29.   document.getElementById("myDIV").removeEventListener("mousemove", myFunction);
  30. }
  31. </script>
  32.  
  33. </body>
  34. </html>
  35.  
Nov 23 '20 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Prasad Dannani | last post by:
Hi, I am currently working on C#. I need to temporarity add remove event handlers which i was done in VB.NET using Addhandler and RemoveHandler functions. How to do it in c# Thanks in...
7
by: sanjana | last post by:
hi i wanna detect if a anything is connected to the usb port I am using system.management class for tht purpose this is my code class usbdetect { public static void Main() {
3
by: Franco, Gustavo | last post by:
How can I remove all event handler from one event without do -=? I won't explain why because is too long, and that the only option left I have right now. I have one event declared for: ...
3
by: Peter Oliphant | last post by:
In general, some controls can have events handlers attached to them, typically via the += operator. How does one remove an event handler added this way? Or, just as good for my purposes, how can...
2
by: Jensen Hoh | last post by:
How can I remove a event handle dynamically? I knew that, it is the proper way to add a event handler for a control Add Handler -----------------------------------------------...
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...
2
by: saeed rezaei | last post by:
Hi we can add user event to class or user control. but can we remove some system event from a control. for examle remove click method from button control
2
by: Dinsdale | last post by:
I appreciate anyones help here. I've done a whole ton of cool things in C# and am having difficulty expressing those things in VB. In a program I wrote in C# (which I no longer have source for) I...
3
by: Samnang | last post by:
Hi, How can I add or remove event of ASP.Net control at runtime, not in Page_Load? I want to click on a button for add or remove the event of another control. Thanks
1
by: john_woo | last post by:
Hi, I'm using prototype 1.6 for observer/attachEventHandler, like Event.observe(myDivId,'mouseup', myFunction); I'm wondering how to remove or stop (or activate/deactivate) this observer...
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: 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:
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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.