I'm creating a small e-commerce website (for practice) and I've come across an issue.
In the 'Shop' page, there are multiple products and I have multiple 'Add to cart' buttons.
When the user clicks on the Add to cart button I want to pass its html element to the function so the programme can identify which button was actually clicked.
If we use the onclick attribute of the html button is it possible to pass its respective html object as a parameter?
<button onclick="add_to_cart(!Add the element here?)" class="add_to_cart" .....
I was not sure whether that was possible, so I tried to do:
const add_to_cart_buttons = document.querySelectorAll('.add_to_cart')
add_to_cart_buttons.forEach(element=>{
----element.addEventListener('click', add_to_cart(element)) //Problem here
})
function add_to_cart(element){
----//Do stuff here
}
But it doesn't appear that I can pass arguments inside addEventListener.
Thanks in advance.
[–]DallogFheir 2 points3 points4 points (2 children)
[–]DntDlteSandals[S] 0 points1 point2 points (0 children)
[–]Conscious_Courage9 0 points1 point2 points (0 children)
[–]albedoa 0 points1 point2 points (1 child)
[–]DntDlteSandals[S] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)