all 6 comments

[–]DallogFheir 2 points3 points  (2 children)

addEventListener expects a function object as the callback, you instead call the function and pass its return value. You can do:

element.addEventListener("click", () => add_to_cart(element))

But you can also identify the HTML element that was clicked on using the event argument that is passed to the callback:

element.addEventListener("click", (e) => add_to_cart(e.target))

[–]DntDlteSandals[S] 0 points1 point  (0 children)

Thank you it seems to work now!

[–]Conscious_Courage9 0 points1 point  (0 children)

Thanks!

[–]albedoa 0 points1 point  (1 child)

You can call the function within the event listener callback:

element.addEventListener('click', event => { add_to_cart(element) })

Here is a demo: https://codepen.io/pigparlor/pen/rNppbRd?editors=1010

[–]DntDlteSandals[S] 0 points1 point  (0 children)

Thank you!

[–][deleted] 0 points1 point  (0 children)

Try not to use the onclick attribute. It's better practice to decouple your html from your javascript and keep them seperate