I have three button. Each of them change backgroud of banner section. I want to set active class on them when clicked. I was trying to take array of buttons and iterate through them but something is not working. Could you tell me what am I doing wrong?
Here is the code:
const banner = document.getElementById('carousel');
const btns = banner.getElementsByClassName('button');
for (const i = 0; i < btns.length; i++) {
btns[i].addEventListener('click', function() {
const current = document.getElementsByClassName('active');
current[0].className = current[0].className.replace('active', '');
this.className += 'active';
})
};
.carousel {
top: 50%;
position: absolute;
}
.carousel .button {
height: 10px;
width: 10px;
border: 2px solid black;
margin: 10px;
border-radius: 9px;
position: relative;
cursor: pointer;
}
.carousel .button .active {
position: absolute;
margin-top: 3px;
margin-left: 3px;
height: 4px;
width: 4px;
border-radius: 5px;
background: #fea100;
}
<div class="carousel" id="carousel">
<div class="button" id="button1">
<div class="active"></div>
</div>
<div class="button" id="button2">
<div></div>
</div>
<div class="button" id="button3">
<div></div>
</div>
</div>
let inotconst i