Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

binding same event twice will trigger twice in jQuery 1.4.2

Tags:

jquery

events

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?

like image 446
Adrian Crapciu Avatar asked Dec 12 '22 20:12

Adrian Crapciu


1 Answers

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/

like image 154
David Hellsing Avatar answered Mar 30 '23 00:03

David Hellsing