Gentlemen, the task is simple: hang up / pick up a class from an array of links. Found code where I don’t understand what role is playing in while el

let elements = document.getElementsByClassName("maneMenu"); for (let i = 0; i < elements.length; i++) { elements[i].onclick = function() { let el = elements[0]; while (el) { // вот здесь туплю if (el.tagName === "A") { el.classList.remove("active"); } el = el.nextSibling; } this.classList.add("active"); } } 
 <div class="icon-bar wrapper_menu"> <a class="maneMenu" href="#"></a> <a class="maneMenu" href="#"></a> <a class="maneMenu" href="#"></a> </div> 

Can you suggest other implementation options on JS?

    1 answer 1

     let elements = document.getElementsByClassName("maneMenu"); for (let i = 0; i < elements.length; i++) {/*прокручиваем в цикле все элементы*/ elements[i].addEventListener('click', function() { /*при клике на элемент */ for (let i = 0; i < elements.length; i++) { elements[i].classList.remove('active'); /*удаляем у всех class active*/ } this.classList.add('active');/*добавляем class active по которому кликнули */ }) } 
     .active { color: green; } 
     <div class="icon-bar wrapper_menu"> <a class="maneMenu" href="#">1111</a> <a class="maneMenu" href="#">2222</a> <a class="maneMenu" href="#">3333</a> </div> 

    • How does this answer the question? And at least I would explain what happens in the code: - / - Alexey Shimansky
    • @ AlekseyShimansky, it seems to me in the literal sense and responds - Air
    • How does it answer the question "what role does it play in while el "? - Alexey Shimansky
    • @ Alexey Shimansky, I wanted to make two options, but it did not work out, the second option is the first one, tell me how to implement two options with one answer? - Air
    • @ Alexey Shimansky, read what he has in the last line question - Air