Binding the same event twice will fire twice in jQuery 1.4.2

if you have something like this:

jQuery(selector).focus(function(){...}); jQuery(selector).focus(function(){...}); 

the focus works twice, is there anyway that I can fix / prevent this?

+4
source share
3 answers

Use data('events') to search for event handlers:

 var isBound = function(el, ev) { var found = false; $.each($(el).data("events"), function(i, e) { if (i === ev) { found = true; } }); return found; } if (!isBound(selector, 'focus')) { $(selector).bind('focus', fn); } 

I think you can use the .one() function in jQuery too, see http://api.jquery.com/one/

+8
source

You can use unbind to remove the previous handler. How you use it depends on why you are trying to avoid using both handlers in the first place.

+3
source

Try using the event namespace and use bind:

 var fnFocus = function(){ ... }; $(selector).bind('focus.namespace',fnFocus); $(selector).unbind('focus.namespace').bind('focus.namespace',fnFocus); 
+1
source

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


All Articles