87

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?

1

19 Answers 19

149

In that case, we can do the following

$('selected').unbind('click').bind('click', function (e) {
  do_something();
});

I had the event firing two times initially, when the page get refreshed it fires four times. It was after many fruitless hours before I figured out with a google search.

I must also say that the code initially was working until I started using the JQueryUI accordion widget.

3
  • 11
    This variant also works: $('.element').unbind("click").on('click', function(e) { ... }); Jul 18, 2014 at 8:38
  • 1
    Just what I was looking for. Also can confirm @RosdiKasim 's comment is correct - that's what I've used.
    – Novocaine
    Feb 3, 2015 at 10:28
  • 1
    This is the answer i was looking for and got it within 5 minutes.. thanks Jun 18, 2019 at 6:59
70

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.

6
  • 32
    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!
    – user276289
    Jun 18, 2010 at 14:38
  • 1
    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
    Jun 18, 2010 at 14:43
  • 18
    @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. Jun 18, 2010 at 14:51
  • 3
    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
    Sep 18, 2010 at 19:53
  • 3
    Thats the correct answer. Many times, doubly loading the same JS file leads to this bug too.
    – user201788
    Mar 14, 2013 at 4:08
43

Strange behaviour which I was experienced also. So for me "return false" did the trick.

$( '#selector' ).on( 'click', function() {
    //code
    return false;
});
5
  • Thank you! In my case I couldn't unbind or turn off my event handler, but simply returning false worked!
    – bergie3000
    Mar 10, 2015 at 6:09
  • 7
    This worked for me as well but I would like to know why.
    – Allen Liu
    Apr 26, 2016 at 21:54
  • Take a look at Noor's answer. Jul 27, 2017 at 15:10
  • 1
    This worked for me. Clicks don't get triggered more than once. However, if the click is bound to a checkbox it doesn't get checked on the click event. Nov 3, 2017 at 20:19
  • Same happened to me, handling a checkbox click, this answer fixed my issue Jan 21, 2021 at 10:47
19

If you use

$( document ).ready({ })

or

$(function() { });

more than once, the click function will trigger as many times as it is used.

1
  • 1
    this fixed mine issue.
    – Kvvaradha
    Jun 15, 2019 at 10:25
17

you can try this.

    $('#id').off().on('click', function() {
        // function body
    });
    $('.class').off().on('click', function() {
        // function body
    });
9

This can as well be triggered by having both input and label inside the element with click listener.

You click on the label, which triggers a click event and as well another click event on the input for the label. Both events bubble to your element.

See this pen of a fancy CSS-only toggle: https://codepen.io/stepanh/pen/WaYzzO

Note: This is not jQuery specific, native listener is triggered 2x as well as shown in the pen.

1
  • so, what should be the fix for this? this is my scenario
    – mars-o
    Aug 28, 2019 at 18:54
9

This can be caused for following reasons:

  1. You have included the script more than once in the same html file
  2. You have added the event listener twice (eg: using onclick attribute on the element and also with jquery
  3. The event is bubbled up to some parent element. (you may consider using event.stopPropagation).
  4. If you use template inheritance like extends in Django, most probably you have included the script in more than one file which are combined together by include or extend template tags
  5. If you are using Django template, you have wrongly placed a block inside another.

So, you should either find them out and remove the duplicate import. It is the best thing to do.

Another solution is to remove all click event listeners first in the script like:

$("#myId").off().on("click", function(event) {
event.stopPropagation();
});

You can skip event.stopPropagation(); if you are sure that the event is not bubbled.

1
  • Thanks for including the obvious #1 in your answer. I can't believe I didn't realize that's what was going on. (facepalm)
    – Josh Coast
    Jan 4, 2021 at 23:27
3

I had the same problem and tried everything but it didn't worked. So I used following trick:

function do_stuff(e)
{
    if(e){ alert(e); }
}
$("#delete").click(function() {
    do_stuff("Clicked");
});

You check if that parameter isn't null than you do code. So when the function will triggered second time it will show what you want.

3
$("#id").off().on("click", function() {

});

Worked for me.

$("#id").off().on("click", function() {

});
2
  • Worked for me too. May 7, 2019 at 14:30
  • Why does this work ? I would like to know. I had a download button which would fire one for first click, two time for second click etc... Mar 22, 2021 at 6:05
2

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.

$('button').click(function(event) {
    event.stopPropagation();
});

I hope this helps.

1
  • This didn't work for me. Something most have been causing the click be be triggered more than once. Nov 3, 2017 at 20:21
2

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.

2

Related to @Stephan's Answer.

In my case, i have both input and label in my .click() listener.

I just replaced the label to div, it worked!

1

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.

0

Unless you want your button to be a submit button, code it as Remove items That should solve your problem. If you do not specify the type for a button element, it will default to a submit button, leading to the problem you identified.

1
  • Sorry - was uploaded strangely. Just add type="button" inside your button tag and that should do it. Jan 7, 2020 at 21:58
0

If you're using AngularJS:

If you're using AngularJS and your jQuery click event is INSIDE THE CONTROLLER, it will get disturbed by the Angular's framework itself and fire twice. To solve this, move it out of the controller and do the following:

// Make sure you're using $(document), or else it won't fire.
$(document).on("click", "#myTemplateId #myButtonId", function () {
   console.log("#myButtonId is fired!");
   // Do something else.
});

angular.module("myModuleName")
   .controller("myController", bla bla bla)
0

One more possibility: 2 events are different in "isTrusted" value.

I just ran into this problem. After doing a little debug, I found that the 2 events are different in "isTrusted" value.


Conditions:

I called element.click() in anotherInputBox's "keydown" event handler. After a keydown, element receive a pair of events, which one is "isTrusted:true", another is "isTrusted:false"

According to MDN (https://developer.mozilla.org/en-US/docs/Web/API/Event/isTrusted), isTrusted is used to determine whether the event is user-triggered(true) or programatically-fired(false).


Solutions:

a. Handle double fire better and block the second event by state in your use case

b. Choose whether you want user-triggered or programatically-fired

c. Do not use click(). Make the click handler a function, and both call the function from event handler directly

0

I was facing the same issue got help from my mentor

$('#button_id').click($.debounce(250, function(e) {
     e.preventDefault();
     var cur = $(this);
     if ($(cur).hasClass('btn-primary')) {
          //do something
     } else {
          // do something else 
     }
 }));

myIssue when the button was clicked hasClass shows true instantly it become false and else part executed fixed by using debounce

0

Working on an old project.

I found out that when there is a radio input in the element and when the click will select the radio input, the jQuery click will be triggered twick and so it the Js

0

I had this issue as clicking on any point on the page causes twice click event.

I checked the source code and find the cause that the jQuery library was defined twice for the final page!

  1. in the master page of the ASP.NET webform project

  2. there was a ScriptManager definition in the Application_Start event of Global.asax file.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.