How can I remove a click event from an element in Javascript?

后端 未结 1 1466
忘了有多久
忘了有多久 2021-01-04 19:08

I have a

element that has a click event attached to it using the following code:

var id = \"someId\";
var elem = document.getElement         


        
相关标签:
1条回答
  • 2021-01-04 19:46

    Move the anonymous click handler function out of the addEventListener call:

    var id = "someId";
    var elem = document.getElementById("elemId");
    var elemEventHandler = function() { someFunction(id); };
    elem.addEventListener("click", elemEventHandler , false);
    

    after which you should be able to:

    var elem = document.getElementById("elemId");
    elem.removeEventListener("click", elemEventHandler , false);
    
    0 讨论(0)
提交回复
热议问题