var panelItem = document.querySelectorAll('.panel-title'), bodyItem = document.querySelectorAll('.panel-body'); panelItem.forEach(function(item, i, panelItem) { item.addEventListener('click', function(e) { if (this.classList.contains('panel-active')) { bodyItem[i].classList.remove('active'); this.classList.remove('panel-active'); } else { item[i].classList.remove('panel-active'); panelItem[i].classList.remove('panel-active'); bodyItem[i].classList.remove('active'); this.classList.add('panel-active'); this.nextElementSibling.classList.add('active'); } }); }); * { padding: 0; margin: 0; } .container { max-width: 700px; margin: 0 auto; padding-top: 20px; } .panel { margin-bottom: 5px; } .panel-title { padding: 10px 15px; background-color: #f5f5f5; border: 1px solid #ddd; font-size: 16px; color: #333; border-radius: 4px; font-family: 'Helvetica'; font-weight: 300; cursor: pointer; margin-bottom: 5px; } .panel-body { padding: 15px; border: 1px solid #ddd; border-bottom-left-radius: 4px; border-bottom-right-radius: 4px; display: none; margin-bottom: 10px; background: #fff; } .panel-active { margin-bottom: 0; border-bottom: 0; border-bottom-left-radius: 0; border-bottom-right-radius: 0; text-decoration: underline; } .active { display: block; } <div class="container"> <div class="panel-group"> <h3 class="panel-title">Group Item #1</h3> <div class="panel-body"> Далеко-далеко за словесными горами в стране, гласных и согласных живут рыбные тексты. Переписывается реторический за, щеке сбить, если большой языкового ему алфавит составитель силуэт меня страну рукопись языком вопроса толку оксмокс она. </div> <h3 class="panel-title">Group Item #2</h3> <div class="panel-body"> Далеко-далеко за словесными горами в стране, гласных и согласных живут рыбные тексты. Переписывается реторический за, щеке сбить, если большой языкового ему алфавит составитель силуэт меня страну рукопись языком вопроса толку оксмокс она. </div> <h3 class="panel-title">Group Item #3</h3> <div class="panel-body"> Далеко-далеко за словесными горами в стране, гласных и согласных живут рыбные тексты. Переписывается реторический за, щеке сбить, если большой языкового ему алфавит составитель силуэт меня страну рукопись языком вопроса толку оксмокс она. </div> </div> </div> Hello. There is one problem, there is an accordion code, now it does not work, it is necessary that if one block is active, then by clicking on another block, the block that was active closes and the block that was clicked must open. The problem here is that I cannot delete a class for all previous headers that have an active class. In particular, an error on the 12th line, I do not understand much why such an error, but I do not understand how to solve it. I hope I clearly explained the essence of the problem; 
itemis not an array, but an element of thepanelItemarray - Dimava