jQuery: Single button click, click event firing two or more times

Viewed 71137

I have HTML input and button elements. I use the buttons to submit forms, open secondary windows, etc. The problem is, a single click is turning into 2 (and sometimes many more) form submissions, or opening two additional browser windows. I've reproduced the problem in multiple web browsers. I've tried switching jQuery versions and that didn't change anything. What could be causing this sort of thing to happen?

8 Answers

Your problem may be happening because you are assigning the same handler to the click event multiple times. I suggest you check that the line where you assign the handler is not being called multiple times inadvertently. Another solution could be a call to unbind (deprecated 3.0) or off (superseeded) first:

$("#myButton").unbind("click").click(myHandler); // deprecated
$("#myButton").off("click").click(myHandler); // superseeded

But without seeing some code I'm just guessing.

for me it was caused by

$(function() {
    $('.some-element').click(function() {
        //event that fires twice
    });

    //some exception-throwing code here
});

jQuery was calling the anonymous ready function twice(?!) because of the exception so the click function would be bound twice.

Related