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
Just do below code it's working absolute fine
$(".addproduct").on('click', function(event){
event.stopPropagation();
event.stopImmediatePropagation();
getRecord();
});
function getRecord(){
$(".addproduct").each(function () {
console.log("test");
});
}
Try making use of the .off()
handler:
$(function () {
$(".archive-link").click(function (e) {
var linkObj = $(this);
var cb = $("#confirm-modal");
cb.find(".modal-body").append("<p>Warning message.</p>");
cb.modal('show');
cb.find(".confirm-yes").click(function () {
$(this).off(); //detach this event
//ajax call yada yada..
}
I attach a click event handler on an OK modal button to act on click
event of the object with class .archive-link
.
On the first click, the event fires only on that .archive-link
object that I asked the function to act on (var linkObj
). But when I click the same link the second time and hit OK on the modal, the event fires on every object that has .archive-link
that I clicked before.
I used the solutions above but unfortunately did not work. It was when I called .off() within the modal OK button click function that the propagation stopped. I hope this helps.
Hi sorry for bump this post just face this problem and i would like to show my case.
My code look like this.
<button onClick="product.delete('1')" class="btn btn-danger">Delete</button>
<button onClick="product.delete('2')" class="btn btn-danger">Delete</button>
<button onClick="product.delete('3')" class="btn btn-danger">Delete</button>
<button onClick="product.delete('4')" class="btn btn-danger">Delete</button>
Javascript code
<script>
var product = {
// Define your function
'add':(product_id)=>{
// Do some thing here
},
'delete':(product_id)=>{
// Do some thig here
}
}
</script>
I just had the same problem. In my case PHP code generated few times the same jQuery code and that was the multiple trigger.
<a href="#popraw" class="report" rel="884(PHP MULTIPLE GENERATER NUMBERS)">Popraw / Zgłoś</a>
(PHP MULTIPLE GENERATER NUMBERS generated also the same code multiple times)
<script type="text/javascript">
$('.report').click(function(){...});
</script>
My solution was to separate script in another php file and load that file ONE TIME.
Simply enter code hereIn JQuery, ones event is triggered you just check number of occurrences of classes in file and use for loop for next logic. for identify number of occurrences of any class, tag or any DOM element through JQuery : var len = $(".addproduct").length;
$(".addproduct").click(function(){
var len = $(".addproduct").length;
for(var i=0;i<len;i++){
...
}
});
Could we see your click handler? You're attaching 5 listeners to 5 different elements. However, when the user clicks on the element, only one event is fired.
$(".addproduct").click(function(){
// Holds the product ID of the clicked element
var productId = $(this).attr('class').replace('addproduct ', '');
addToCart(productId);
});
If this solution doesn't work I'd like to look at your click handler.