I have a list of buttons.
I need to add a clickEvent to each of them, when clicked it needs to check if there's another button with the class(btnActive), remove it and add the class to the clicked button.
document.querySelectorAll('.btn').forEach(function(el) {
el.addEventListener('click', function(e) {
let i = 0;
for (i = 0; i < btnAll.length; i++) {
if (i.classList.contains('btnActive')) {
i.classList.remove('btnActive');
}
}
el.classList.add('btnActive');
});
});
<div class="btnContainer">
<div class="btn">
<span class="btnActive">
0
</span>
</div>
<div class="btn">
<span>
1
</span>
</div>
<div class="btn">
<span>
2
</span>
</div>
<div class="btn">
<span>
3
</span>
</div>
</div>
I have that little JS block and I can't seem to get it to work