Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

click() event is calling twice in jQuery

Tags:

jquery

I setup a link element and called its click event in jQuery but the click event is calling twice, please see below the code of jQuery.

$("#link_button")
.button()
.click(function () {
   $("#attachmentForm").slideToggle("fast");
});
like image 964
domlao Avatar asked Oct 06 '22 15:10

domlao


People also ask

Why is this jquery Ajax click event firing multiple times?

This happens because somewhere in your code, you're rebinding the event handler without first unbinding it.

What is the difference between .click and .on (' click in jquery?

. click events only work when element gets rendered and are only attached to elements loaded when the DOM is ready. . on events are dynamically attached to DOM elements, which is helpful when you want to attach an event to DOM elements that are rendered on ajax request or something else (after the DOM is ready).

What is .click in Javascript?

The HTMLElement. click() method simulates a mouse click on an element.


2 Answers

Make sure and check that you have not accidentally included your script twice in your HTML page.

like image 253
Scott Avatar answered Oct 08 '22 05:10

Scott


Make un unbind before the click;

$("#link_button").unbind('click');
$("#link_button")
.button()
.click(function () {
   $("#attachmentForm").slideToggle("fast");
});
like image 80
TheSystem Avatar answered Oct 08 '22 04:10

TheSystem