Is it possible to remove all event handlers of a given element in javascript?

前端 未结 2 527
失恋的感觉
失恋的感觉 2020-12-15 15:08

I would like to remove ALL handlers for a given event type. Let\'s say I\'ve added twice \"onclick event\" to a button and now I would like to return back to the original st

相关标签:
2条回答
  • 2020-12-15 15:57

    If you have only one child element under your element's parent (or if you don't mind all sibling's event handlers lost too):

    elem.parentElement.innerHTML = elem.parentElement.innerHTML;
    

    Tested in Chrome 49, FF 44, IE 11

    It removes all 'addEventListener'-s.

    0 讨论(0)
  • 2020-12-15 15:59

    It might be a good idea to use jQuery or a similar framework to manage all event handlers. This will give you easy-to-use, unobtrusive functions to add and remove event handlers:

    $(...).on('click', function() { ... });
    $(...).off('click');
    // or, to unbind all events:
    $(...).off();
    
    0 讨论(0)
提交回复
热议问题