views:

269

answers:

1

Hello everyone

I have the following problem with this code:

<button id="delete">Remove items</button>

$("#delete").button({
     icons: {
             primary: 'ui-icon-trash'
     }
}).click(function() {
     alert("Clicked");
});

If I click this button, the alert show up two times. It's not only with this specific button but with every single button I create.

Can someone tell me what I do wrong?

Thanks for your help in advance.

+1  A: 

Your current code works, you can try it here: http://jsfiddle.net/s4UyH/

You have something outside the example triggering another .click(), check for other handlers that are also triggering a click event on that element.

Nick Craver
You are right! I put an .unbind("click") in front of the .click(). Now it is working properly. I have to search the element that triggers the event a second time...Thank you very much!
That sounds more like you were binding the same click event twice. jQuery will queue the bindings so if you bind several click events to the same element, when you click it, they will all fire.The slight difference would be the click event was fired twice vs the same click event bound to the element twice.
MacAnthony
@user276289 - It's possible that your code in it's entirety is running more than once, make sure it's only included one time in the page.
Nick Craver
Thanks Nick Craver! That was exactly my problem, I had my code inlined on a jQuery dialog, so it was executed again when the jQuery UI dialog was rendered.
James