For each Loop

let accBox = document.querySelectorAll('.accBox');
for (i = 0; i < accBox.length; i++) {
accBox[i].addEventListener('click', function () {
accBox.forEach(el => {
el.classList.remove('active'); });
this.classList.toggle('active');
});
}
let accBox = document.querySelectorAll('.accBox');
for (i = 0; i < accBox.length; i++) {
accBox[i].addEventListener('click', function () {
accBox.forEach(el => {
el.classList.remove('active'); });
this.classList.toggle('active');
});
}
How can I convert this on to forEach code
1 Reply
Sinc02
Sinc023y ago
for(let box of accBox){
....
}
for(let box of accBox){
....
}
And replace every accBox[i] with box Wait that's for of Eeh you already did forEach right? Just do the same for the top one
accBox.forEach((box) => {
...
})
accBox.forEach((box) => {
...
})
And replace every accBox[i] with box
Want results from more Discord servers?
Add your server