I have many these links in my template
<a href="javascript:void(0);" class="add_to_cart" data-slug="{{ product.slug }}">Add</a>
And need click to the link and get certain data-slug
. I try use JS
and get all links so.
var add_to_cart = document.getElementsByClassName('add_to_cart');
After I try for
for(var i = 0; i < add_to_cart.length; i++) {
product_slug = add_to_cart[i].getAttribute('data-slug')
add_to_cart[i].onclick = function() {
console.log(product_slug)
}
}
And after click always console.log
have last data-slug
latest link in my template. How to fix it and get the data-slug
of the item I clicked on. Help me, please.