Skip to content
Advertisement

addEventListener not working for a HTML Button that is within a Django for loop

I am trying to call a function when an HTML button is clicked, but right now, a console.log is supposed to run when the button is clicked, but nothing appears in the console. This button is within a for loop in Django and is being selected via querySelector through its class. I want only the specific button that is clicked to be selected, but I do not know if anything is being selected because nothing happens when the button is clicked.

javascript

JavaScript

html

JavaScript

Advertisement

Answer

querySelector only returns the first element that matches the query. Use querySelectorAll to get all elements that are matches. Loop over result and add the event listeners per button.

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement