Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delegate event firing twice

This delegate event is firing twice (not always, sometimes).

client.bindButtonClickFunction = function(){

    $("#client-plugin").delegate(".client-button", "click", function()
    {
        var id = this.id.split('-')[2];
        client.retrieveMessageByID(id);
    });
};

I call the function after inserting all the ".client-button"'s.

Any thoughts on how to stop it? I tried event.stopPropagation(), and also undelegating and re-delegating to no avail.

This is in Chrome, as part of a Chrome plugin.

like image 402
Sara Chipps Avatar asked Nov 28 '22 07:11

Sara Chipps


2 Answers

Depending on how you register the delegate, you might want to do:

$("#client-plugin").undelegate('event').delegate('event', ...)

Also, try to add a return false from your handler.

like image 68
Mrchief Avatar answered Dec 09 '22 15:12

Mrchief


You need to stop immediate propagation

   $("#client-plugin").on(".client-button", "click", function (e) {
    e.stopImmediatePropagation();
    var id = this.id.split('-')[2];
    client.retrieveMessageByID(id);
});
like image 43
Rana Avatar answered Dec 09 '22 15:12

Rana