if you got something like this:
jQuery(selector).focus(function(){...});
jQuery(selector).focus(function(){...});
the focus will trigger twice, is there anyway that I can correct/prevent that?
if you got something like this:
jQuery(selector).focus(function(){...});
jQuery(selector).focus(function(){...});
the focus will trigger twice, is there anyway that I can correct/prevent that?
You can use unbind to remove the earlier handler. How you would use it depends exactly on why you are trying to avoid having both handlers fire in the first place.
Use the data('events')
to find 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, have a look at http://api.jquery.com/one/
Try using event namespace and use bind:
var fnFocus = function(){ ... };
$(selector).bind('focus.namespace',fnFocus);
$(selector).unbind('focus.namespace').bind('focus.namespace',fnFocus);