при клике ошибка Cannot read property 'classList' of undefined at HTMLLIElement.y
let nth = document.querySelectorAll(".nth")
for(i=0;i<n.length;i++){
n[i].onclick = y;
function y(){
nth[i].classList.toggle("nth-n");
}
}
при клике ошибка Cannot read property 'classList' of undefined at HTMLLIElement.y
let nth = document.querySelectorAll(".nth")
for(i=0;i<n.length;i++){
n[i].onclick = y;
function y(){
nth[i].classList.toggle("nth-n");
}
}
Беда была с названием переменной. Сначала name, потом n, потом nth
let nth = document.querySelectorAll(".nth");
for(let i=0;i<nth.length;i++){
nth[i].onclick = function(){
nth[i].classList.toggle("nth-n");
}
}
body{
display: grid;
grid-gap: 10px;
grid-template-columns: repeat(2, 1fr);
}
.nth{
height: 100px;
background: #cda;
cursor: pointer;
}
.nth-n{
background: #acd;
}
<div class="nth"></div>
<div class="nth"></div>
<div class="nth"></div>
<div class="nth"></div>