Overriding previously linked click events

When I use this code with an element whose id is "foobar":

$("#foobar").click(function () { alert("first"); });
$("#foobar").click(function () { alert("second"); });

I get two warnings: "first" and "second" second.

How to specify a click event that also clears any previous click events associated with an element? I want the latter to $("#foobar").click(...)delete any previously related events.

+3
source share
2 answers

You can untie events already attached to this element, and then attach a second event handler, so it will be the only one (note the unbind method).

$("#foobar").unbind("click").click(function() { alert("second"); });
+9
source
$("#foobar").click(function () { alert("first"); });
$("#foobar").unbind('click').click(function () { alert("second"); });

unbind(). , .

+3

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


All Articles