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.
What am I doing wrong?
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 aclick
event on that element.Worked for me.
If you use
or
more than once, the click function will trigger as many times as it is used.
I've found that binding an element.click in a function that happens more than once will queue it so next time you click it, it will trigger as many times as the binding function was executed. Newcomer mistake probably on my end but I hope it helps. TL,DR: Make sure you bind all clicks on a setup function that only happens once.
in my case, i was using the change command like this way
$(document).on('change', '.select-brand', function () {...my codes...});
and then i changed the way to
$('.select-brand').on('change', function () {...my codes...});
and it solved my problem.
Just like what Nick is trying to say, something from outside is triggering the event twice. To solve that you should use event.stopPropagation() to prevent the parent element from bubbling.
I hope this helps.