var toggle = document.querySelectorAll(".faq__accordion-toggle");
var answer = document.querySelectorAll(".faq__accordion-answer");
toggle[0].addEventListener("click", function (event){
event.preventDefault();
console.log("Клик по ссылки");
if (answer[0].classList.contains("faq__item-answer-show")) {
answer[0].classList.remove("faq__item-answer-show");
}
else {
answer[0].classList.add("faq__item-answer-show");
answer[1].classList.remove("faq__item-answer-show");
answer[2].classList.remove("faq__item-answer-show");
answer[3].classList.remove("faq__item-answer-show");
answer[4].classList.remove("faq__item-answer-show");
answer[5].classList.remove("faq__item-answer-show");
answer[6].classList.remove("faq__item-answer-show");
answer[7].classList.remove("faq__item-answer-show");
answer[8].classList.remove("faq__item-answer-show");
answer[9].classList.remove("faq__item-answer-show");
answer[10].classList.remove("faq__item-answer-show");
answer[11].classList.remove("faq__item-answer-show");
}
});
for( var i = 11; 0 <= i; i-- ){
answer[i].classList.remove("faq__item-answer-show");
}
var toggle = document.querySelectorAll(".faq__accordion-toggle");
var answer = document.querySelectorAll(".faq__accordion-answer");
toggle[0].addEventListener("click", function (event){
event.preventDefault();
console.log("Клик по ссылки");
if (answer[0].classList.contains("faq__item-answer-show")) {
answer[0].classList.remove("faq__item-answer-show");
}
else {
answer[0].classList.add("faq__item-answer-show");
for (var i = 1; i <= 11; i++) { //Вот здесь
answer[i].classList.remove("faq__item-answer-show");
}
}
});
<!DOCTYPE html>
<html>
<head>
<title>лишки</title>
<style>
.nav-list__item {
cursor: pointer;
background-color: yellow;
color: green
}
.nav-list {
width: 150px;
}
.nav-list__item-blue {
color: blue;
background-color: orangered;
}
</style>
</head>
<body>
<ul class="nav-list">
<li class="nav-list__item nav-list__item-blue">Пункт меню</li>
<li class="nav-list__item">Пункт меню</li>
<li class="nav-list__item">Пункт меню</li>
<li class="nav-list__item">Пункт меню</li>
<li class="nav-list__item">Пункт меню</li>
</ul>
<script>
var link = document.querySelectorAll(".nav-list__item");
var linkArray = Array.prototype.slice.call(link);
linkArray.forEach(function(item, index, array){
linkArray[index].addEventListener("click", function(event) {
event.preventDefault();
console.log("клик по пункту меню" + [index]);
if (!link[index].classList.contains("nav-list__item-blue")) {
link[index].classList.add("nav-list__item-blue");
for (i=0; i < linkArray.length; i++) {
if (i !== index) {
link[i].classList.remove("nav-list__item-blue");
}
}
} else {
link[index].classList.remove("nav-list__item-blue");
}
});
});
</script>
</body>
</html>