I have multiple classes on a page of the same name. I then have a .click() event in my JS. What I want to happen is the click event only happen once, regardless of multiple classes on my page.
The scenario is that I am using AJAX to add to cart. Sometimes on the home page there might be a featured product and a top offer which means that the same class .add .#productid# is there and when clicked the add to cart AJAX is fired twice.
I thought about making 'click areas' so I would have .container-name .add .#pid# therefore giving unique clicks.
Is this the only solution?
<div class="addproduct 151">product info</div>
<div class="addproduct 151">product info</div>
<div class="addproduct 151">product info</div>
<div class="addproduct 151">product info</div>
<div class="addproduct 151">product info</div>
$(".addproduct").click(function(){//do something fired 5 times});
when you click div with addproduct class one event is fired for that particular element, not five. you're doing something wrong in you code if event is fired 5 times.
I just had the same problem. In my case PHP code generated few times the same jQuery code and that was the multiple trigger.
(PHP MULTIPLE GENERATER NUMBERS generated also the same code multiple times)
My solution was to separate script in another php file and load that file ONE TIME.
I had the same problem. The cause was that I had the same jquery several times. He was placed in a loop.
For this reason was firing multiple times
your event is triggered only once... so this code may work try this
I think you add click event five times. Try to count how many times you do this.
This question have been resolved and also got a lot of responses, but i want to add something to it. I was trying to figure out, why my click element his firing 77 times, and not one time.
in my code, i had an each, running a json response and displaying it as divs with buttons. And then i declared the click event inside the each.
If you write your your code like this, the class .test-button will get multiple click events. For example, my data has 77 lines, so the each will run 77 times, that means i will decline the click event on the class 77 times. When you click the element, it will be fired 77 times.
But if you wirte it like this:
you are declaring the click element after the each. That means, the each will run its 77 times, and the click element will be declared only one time. So, if you click the element, it will be fired only one time.