How to remove click event using jquery

I have jquery code to set the click event as follows: $ ("# somediv"). click (function () {alert ('test')});

How to remove the above click event? It seems that the .click () method will always add existing ones.

+4
source share
2 answers

Use

$('#somediv').unbind('click'); 

If you want to remove this function, you need a link to it:

 var test = function() {alert('test');}; $("#somediv").click(test); window.setTimeout(function () { $('#somediv').unbind('click', test); }, 10000); 

http://api.jquery.com/unbind/

+8
source
 You can use off() method as well. on() will be used to create event and off() will be used to remove event. function clickEvent() { $("#somediv2").show().fadeOut("slow"); }; To **remove** events you can use like this, $('#somediv').off("click", "#somediv1", clickEvent); To **add** events you can use like this, $('#somediv').on("click", "#somediv1", clickEvent); http://api.jquery.com/off/ http://api.jquery.com/on/ 
0
source

Source: https://habr.com/ru/post/1315733/


All Articles