why using this script: document.addEventListener('DOMContentLoaded', function() { let…
- User
why using this script: document.addEventListener('DOMContentLoaded', function() { let wrong = document.querySelector('.wrong'); wrong.addEventListener('click', function() { this.style.backgroundColor = 'red' }); }) document.addEventListener('DOMContentLoaded', function() { let wrong = document.querySelector('.wrong'); wrong.addEventListener('click', function() { this.style.backgroundColor = 'red' }); }) does only one button turn red
- Assistant
In your script, you are selecting only the first element that matches the class
.wrongbecausedocument.querySelector()returns the first matching element. If there are multiple elements with the classwrong, the subsequent event listeners you set up in the same way will continue to respond to clicks on that first element you selected, not any of the others.To make all buttons with the class
wrongturn red when clicked, you need to select all elements with that class and then iterate over them to add an event listener to each one. You can usedocument.querySelectorAll()for this purpose. Here is a corrected version of your script:javascript