Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

104
Visualizações
Click to change image across multiple items

I'm trying to set up a javascript function that will take a down arrow image and swap it with an up arrow image when clicked. It worked when the image had an id, but because I have a couple menu items that uses the arrow image, I wanted to try to get it to work as a class name or just regular name, but I can't get the function to change the source of the image when it's not an id. Can anyone help? Here is my current HTML markup:

<div class="header__links hide-for-mobile" >
                <a id="subMenu" href="#">Features <img class="downArrow" src="/assets/images/icon-arrow-down.svg"></a>
                <a href="#">Company</a>
                <a href="#">Careers</a>
                <a href="#">About</a>
            </div>

And here is my JS

const subMenu = document.querySelector('#subMenu');
subMenu.addEventListener('click', function() {
    if(subMenu.classList.contains('subOpen')) {
        subMenu.classList.remove('subOpen');
        document.getElementsByClassName('downArrow').src="/assets/images/icon-arrow-down.svg";
    }

    else {
        subMenu.classList.add('subOpen');
        document.getElementsByClassName('downArrow').src="/assets/images/icon-arrow-up.svg";
    }
})
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Right now since your markup only contains a single element with the downArrow class you could update your code to:

const subMenu = document.querySelector('#subMenu');
subMenu.addEventListener('click', function() {
    if(subMenu.classList.contains('subOpen')) {
        subMenu.classList.remove('subOpen');
        document.getElementsByClassName('downArrow')[0].src="/assets/images/icon-arrow-down.svg";
    }

    else {
        subMenu.classList.add('subOpen');
        document.getElementsByClassName('downArrow')[0].src="/assets/images/icon-arrow-up.svg";
    }
})

But a better way to do this would be to loop through the results of the getELementsByClassName method. You could do that with something like:

const subMenu = document.querySelector('#subMenu');
subMenu.addEventListener('click', function() {
    if(subMenu.classList.contains('subOpen')) {
        subMenu.classList.remove('subOpen');
        Array.from(document.getElementsByClassName("downArrow")).forEach(
            function(element, index, array) {
                element.src="/assets/images/icon-arrow-down.svg";
            }
        );
    }

    else {
        subMenu.classList.add('subOpen');
        Array.from(document.getElementsByClassName("downArrow")).forEach(
            function(element, index, array) {
                element.src="/assets/images/icon-arrow-up.svg";
            }
        );
    }
})
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda