Как удалить style из всех остальных button, кроме тех, на которые я только что нажал?
var box = document.getElementsByClassName('box')
for (var i = 0; i < box.length; i++) {
box[i].addEventListener('click',function(event){
event.target.style.backgroundColor = 'red'
})
}
Ответы (1 шт):
Автор решения: OPTIMUS PRIME
→ Ссылка
let box = document.querySelectorAll(".box");
for (let i = 0; i < box.length; i++) {
box[i].addEventListener("click", highlight);
}
function highlight() {
for (let i = 0; i < box.length; i++) {
box[i].style.backgroundColor = ""; // Удалить стили у всех элементов,
}
this.style.backgroundColor = "red"; // Добавить кликнутому элементу.
}
.box { display: inline-block; padding: 5px; border: 1px solid red; cursor: pointer }
<div class="box">00</div>
<div class="box">00</div>
<div class="box">00</div>
<div class="box">00</div>