In JavaScript, we can add multiple functions to body's onload or a button's onclick. Is it possible to remove one of them, if we have the reference to the function ?
If possible, how to acplish the same in jQuery and also plain JavaScript ?
In JavaScript, we can add multiple functions to body's onload or a button's onclick. Is it possible to remove one of them, if we have the reference to the function ?
If possible, how to acplish the same in jQuery and also plain JavaScript ?
Share Improve this question asked Nov 26, 2012 at 10:43 Amogh TalpallikarAmogh Talpallikar 12.2k15 gold badges84 silver badges135 bronze badges 6- Yes it is. – James Allardice Commented Nov 26, 2012 at 10:44
- Is it just a spec ? or it is supported by major browsers. – Amogh Talpallikar Commented Nov 26, 2012 at 10:45
-
Did you read the page I linked to? It explains that. Also note that with jQuery you can use
.off()
or.unbind()
. – James Allardice Commented Nov 26, 2012 at 10:46 - Will it work even if the event was added without the use of addEvent Listener ? If I add it to onclick of a button, then can I pass 'click' in removeEvent Listener for type for the same element. – Amogh Talpallikar Commented Nov 26, 2012 at 10:51
- No. See the answer by David for a solution to that. – James Allardice Commented Nov 26, 2012 at 10:51
1 Answer
Reset to default 13Yes, but it depends on how the handler was attached in the first place.
If you used addEventListener
, you can unbind using removeEventListener
.
If you used jQuery, you can use .unbind()
.
If you used attachEvent
, you can use detachEvent()
.
If you used onclick
or other onxxx
functions, you can remove them by assigning null
, f.ex elem.onclick = null
.