Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery bind firing multiple times?

$(".container").on("contextmenu", ".photos-bottom .albums li", function(e) {

$('html').bind('click', function (event) {
    alert(id);
});

return false;
});

when I right click (for the contextmenu) multiple times and then left click html once, it triggers the alert the number of times that I right clicked.

So if I right click once, then left click, it shows a popup once. If I right click three times, then left click, it shows the popup three times.

Why is this so?

like image 323
Dylan Cross Avatar asked Jan 28 '12 18:01

Dylan Cross


2 Answers

$('html').unbind('click').bind('click') fixed it.

like image 66
Dylan Cross Avatar answered Nov 19 '22 21:11

Dylan Cross


Because your click event is being bound every time a context menu event occurs, you're actually adding an additional bind each time you right click. This is the reason for the ever-growing number of event executions.

You should either:

a) unbind the event when the context menu is closed, or

b) bind the click event outside of your contextmenu callback function.

like image 45
Kristian Avatar answered Nov 19 '22 21:11

Kristian