1

I want to remove only the mouseup event listners from a selected HTML element.

I used the below code but it will remove all listners.

var old_element = divs[d];
                var new_element = old_element.cloneNode(true);
                old_element.parentNode.replaceChild(new_element, old_element);

this is how i attach event listners.

var divs = document.getElementsByTagName('body');// to enhance the preformance
for(var d in divs) { 
        try{             
            if (divs[d].addEventListener) {
                 divs[d].addEventListener('mouseup',callHighlight);
            } else {
                divs[d].attachEvent('mouseup', callHighlight);
            } 
        }catch(err){
            //alert(err.message);
        }
}       
1
  • It won't remove inline listeners, nor those added using attachEvent in IE. Commented Oct 14, 2013 at 2:26

3 Answers 3

1

You should use removeEventListener instead of replacechild which will obviously remove all events.

 old_element.removeEventListener('mouseup', handler);
Sign up to request clarification or add additional context in comments.

7 Comments

I used that also. But in my code there were several mouseup events. I add several mouseup listeners to an element. Will your code remove all of those?
That only works if a reference to the listener function is available to pass to removeEventListener, so it won't work where a function expression is used in the assignment.
Yup how are you attaching the events? if you use anonymous function instead of a func reference this wont work. Show your event attachment part, are you using attributes?
@PSL i showed the event attachment part in my original question.
@vidudaya Yes you can use callHighlight in the removeListened. See the example in the fiddle in my prev comment.
|
1

When cloning an element, listeners added using addEventListener or by direct property assignment (element.onclick = fn;) are removed, but in–line listeners and those added using IE's attachEvent are not.

In your scenario where listeners are added by reference and also possibly using attachEvent, you are best to remove them using removeEventListener and detachEvent. So you might like to create add and remove functions like:

function addEvent(element, event, fn) {
  if (element.addEventListener) {
    element.addEventListener(event, fn, false);
  } else if (element.attachEvent) {
    element.attachEvent('on' + event, fn);
  }
}

function removeEvent(element, event, fn) {
  if (element.removeEventListener) {
    element.removeEventListener(event, fn);
  } else if (element.detachEvent) {
    element.detachEvent('on' + event, fn);
  }
}

Note that there are some significant differences between addEventListener and attachEvent, the most important are that in the latter, this is not set to the element whose handler is calling the function and a reference to the event isn't passed as the first argument to the listener. So the listener function ends up looking like:

function foo(evt) {
  evt = evt || window.event;
  var target = evt.target || evt.srcElement;
  ...
}

There are ways around this, but they introduce more issues. Keep it simple if you can.

1 Comment

Looks like using solely addEventListener and removeEventListener is the way to go in the modern world: developer.mozilla.org/en-US/docs/Web/API/EventTarget/… caniuse.com/#search=removeeventlistener
0

By default all event listeners are null, so simply just reset it. Problem is that all your mouseup events are registered to the body, so therefore you won't be able to drop the event without first stopping the event from bubbling to the body. You can solve that problem with, stopPropagation()

 old_element.onmouseup = function (e) {
   // event won't go up to the body tag
   e.stopPropagation();
   return null;
 };

or

 function kill (e) {
  e.stopPropagation(); 
  return null; 
 }

 old_element.onmouseup = kill;
 second_element.onmouseup = kill;

JSFIDDLE

2 Comments

The OP is attaching listeners using addEventListener and attachEvent. Assignment to the related property does not overwrite such listeners.
@RobG see when you say that i get confused, bc it works in this fiddle

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.