So I'm trying to make it where all the boxes change color whenever I click on the button and make it run continuously until I click the button again to stop it.
I tried querySelectorAll() but it only changes the first box's color
Here is my codepen showing what is happening https://codepen.io/designextras/pen/bGEowbO
Here is the javascript below
const colorBtn = document.querySelector('#btn-1')
const boxes = document.querySelector('.box')
function getRandomColor() {
let letters = '0123456789ABCDEF';
let color = '#';
for (let i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
colorBtn.addEventListener('click', function(e) {
let newColors = getRandomColor()
boxes.style.background = newColors
})
Edit: I fixed it by adding a forEach()
colorBtn.addEventListener('click', function(e) {
boxes.forEach(color => {
let newColors = getRandomColor()
color.style.background = newColors
})
})