Monzingo
Monzingo

Reputation: 411

jquery function keeps repeating by one on button click

I am very new to jquery and I have been debugging this code through chrome's debugging tool. Every time I click the button, the function works, but if I click it again the function executes twice, and if I click it again it executes 3 times. I'm not sure why this could be happening. Code is below. It might have to do with the fact that the project is webforms and there are update panels within it?

 $(document).ready(function () {
    $("#BlueButton").click(function () {
        $(this).click(function () {
            alert('Hello');
        }); 
    });
});

Thanks.

Upvotes: 1

Views: 836

Answers (1)

Rajaprabhu Aravindasamy
Rajaprabhu Aravindasamy

Reputation: 67207

Yes, It will happen like that. since you coded in that way. Remove the event binding inside of the main("#BlueButton") click event.

$(document).ready(function () {
    $("#BlueButton").click(function () {
            alert('Hello');
    });
});

Event will be fired in order as much times you bound it. So do not bind duplicate events.

Upvotes: 2

Related Questions