@LuisJimenez19
Posted
Hello, congratulations for finishing the challenge.
It would be better if you captured all the buttons and then iterated them with an array.forEach
so you would save more code and you would not be repeating yourself, and to be able to remove the styles from the ones that are not selected you could do something like this:
function selectedOption(arr, current){
arr.forEach((item)=>{
item.classList.remove("active") // remove styles all buttons
})
current.classList.add("active") // add style current button
}
document.querySelectorAll("button").forEach((button,index,arr) => {
button.addEventListener("click", ()=>{
selectedOption(arr, arr[index])
})
})
.active{
background-color: rgb(149, 158, 172);
color: white;
}
I hope my contribution has been useful, let's keep trying.
Marked as helpful