The click () event fires twice in jquery

I set the link element and fire its click event in jquery, but the click event fires twice, see the jquery code below.

$("#link_button") .button() .click(function () { $("#attachmentForm").slideToggle("fast"); }); 

Please inform.

Thank.

+91
jquery
Jul 18 '11 at 11:00
source share
24 answers

Make sure you don't accidentally include your script in your HTML page twice.

+196
Jul 18 '11 at 11:25
source share

Cancel unlock before clicking;

 $("#link_button").unbind('click'); $("#link_button") .button() .click(function () { $("#attachmentForm").slideToggle("fast"); }); 
+64
Jul 18 '11 at 11:32
source share

This means that your code has included the jquery script twice. But try the following:

 $("#btn").unbind("click").click(function(){ //your code }); 
+39
Aug 20 '13 at 16:49 on
source share

I tried, e.stopImmediatePropagation() ; It seems to work for me.

+26
Apr 03 '15 at 10:06
source share

In my case, I used below script to solve the problem

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

off() unbinds all events to #id . If you want to cancel only the click event, use off('click') .

+16
Nov 27 '15 at 5:24
source share

Just call .off () right before calling .on ().

This will remove all event handlers:

 $(element).off().on('click', function() { // function body }); 

To remove only registered 'click' event handlers:

 $(element).off('click').on('click', function() { // function body }); 
+7
Jan 04 '16 at 7:16
source share

Rather, a general solution to a two-click problem:

 e.stopPropagation() 

at the end of your function (if you use function(e) ). This prevents a bubble event that could lead to a second execution of the function.

+6
Jul 17 '14 at 15:08
source share

I had the same problem, but you can try changing this code

 $("#link_button") .button() .click(function () { $("#attachmentForm").slideToggle("fast"); }); 

:

 $("#link_button").button(); $("#link_button").unbind("click").click(function () { $("#attachmentForm").slideToggle("fast"); }); 

For me, this code solved the problem. But be careful not to accidentally include your script twice in your HTML page. I think that if you do these two things correctly, your code will work correctly. Thanks

+3
Jul 08 '13 at 16:02
source share

try it

 $('#ddlSupervisor').live('change', function (e) { if (e.handled !== true) { //this makes event to fire only once getEmployeesbySupervisor(); e.handled = true; } }); 
+3
Aug 29 '14 at 13:38 on
source share

This is definitely a bug, especially on FireFox. I searched alot, tried all of the above answers and finally got this as a mistake of many experts on SO. So, I finally came up with this idea by declaring a variable like

 var called = false; $("#ColorPalete li").click(function() { if(!called) { called = true; setTimeout(function(){ //<-----This can be an ajax request but keep in mind to set called=false when you get response or when the function has successfully executed. alert('I am called'); called = false; },3000); } }); 

Thus, it first checks, rather, the function was called earlier or not.

+2
Jan 23
source share

Adding e.preventDefault(); at the beginning of my function worked for me.

+2
Aug 19 '15 at 15:59
source share

This is an old question, but if you use event delegation, this is what caused it to me.

After deleting .delegate-two it stopped execution twice. I believe this happens if both delegates are on the same page.

 $('.delegate-one, .delegate-two').on('click', '.button', function() { /* CODE */ }); 
+2
May 17 '18 at 6:46
source share

this piece of code does not contain anything bad. This is another part of your script, as @karim said

+1
Jul 18 '11 at 11:27
source share

In my case, it worked fine in Chrome, and IE called .click() twice. if that was your problem, then I fixed it by returning false after the .click() event was .click()

  $("#txtChat").keypress(function(event) { if (event.which == 13) { $('#btnChat').click(); $('#txtChat').val(''); return false; } }); 
+1
Aug 11 '14 at
source share

The unbind call solved my problem:

 $("#btn").unbind("click").click(function() { // your code }); 
+1
Dec 28 '15 at 13:45
source share

When I use this method on the download page with jquery, I write $('#obj').off('click'); before setting the click function, so the bubble does not occur. It works for me.

0
Oct 27 '14 at 17:50
source share

I was tricked by a selection corresponding to several elements, so each clicked. :first helped:

 $('.someClass[data-foo="'+notAlwaysUniqueID+'"]:first').click(); 
0
Jan 04 '15 at 18:56
source share

I also had this problem in FF. However, my tag is attached to the <a> tag. Although the <a> tag will not go anywhere, it still double-clicks. Instead, I replaced the <a> tag with the <span> , and the double-click problem disappeared.

Another alternative is to completely remove the href attribute if the link goes nowhere.

0
Jan 07 '15 at 21:15
source share

I ran into this problem because my $(elem).click(function(){}); the script was embedded in a div for which style="display:none;" .

When the css display has been switched to a block, the script will add an event listener a second time. I moved the script to a separate .js file and the duplicate event listener was no longer running.

0
Jun 12 '15 at 22:05
source share

My simple answer was to turn the click binding into a function and call it from the onclick element - processed the pleasure! while none of the above did

0
Mar 02 '17 at 21:17
source share

I'm not sure why, but I had this problem with

 $(document).on("click", ".my-element", function (e) { }); 

When I changed it to

 $(".my-element").click(function () { }); 

The problem is resolved. But there is definitely something wrong with my code.

0
Apr 10 '17 at 10:55
source share

I solved this problem by changing the type of the item. I put an input instead of a button, and this problem no longer occurs.

instesd of:

 <button onclick="doSomthing()">click me</button> 

replaced by:

 <input onclick="doSomthing()" value="click me"> 
0
Dec 06 '17 at 17:27
source share

There was the same problem. It worked for me -

 $('selector').once().click(function() {}); 

Hope this helps someone.

-one
Apr 04 '13 at 16:30
source share

In my case, the HTML was laid out as follows:

 <div class="share"> <div class="wrapper"> <div class="share"> </div> </div> </div> 

And my jQuery was like this:

 jQuery('.share').toggle(); 

It embarrassed me because nothing happened. The problem was that the internal .share toggle would override the external .share toggle .

-one
Jan 14 '15 at 16:26
source share



All Articles