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

140
Visualizações
Wrapped Img is not being picked up in JS

I've been stuck on this for a few days. I've tried different selectors and unwrapping the img in the div if that was the problem but no luck. I've been trying to make an accordion. I'm trying to add a class of "rotate" to the img with the class of "arrow". So that when the question tag is clicked, the arrow img will also rotate.

const questionTag = document.querySelectorAll('.question')

questionTag.forEach(questionTag => {
    questionTag.addEventListener('click', () => {
        if (questionTag.classList.contains('open')) {
            questionTag.classList.remove('open');
        } else {
            const questionTagOpen = document.querySelectorAll('.open');

            questionTagOpen.forEach((questionTagOpen) => {
                questionTagOpen.classList.remove('open');
            });
            questionTag.classList.add('open');
        }
    });
});
.question + .answer {
    display: none;
    overflow: hidden;
    transition: all ease 1s;
}


.question.open + .answer {
    display: block;
}


.arrow.rotate {
    transform: rotate(180deg);
}
<div class="container">
  <div class="question">How many team members can I invite?
    <img class="arrow" src="./images/icon-arrow-down.svg">
  </div>
  <div class="answer">You can invite up to 2 additional users on the Free plan. There is no limit on 
    team members for the Premium plan.</div>
</div>

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You're missing [0] in your code.

The arrowTag comes from document.querySelectorAll(), which returns a NodeList, you need to specify the element from that NodeList:

var questionTag = document.querySelectorAll('.question')

questionTag.forEach(questionTag => {
    questionTag.addEventListener('click', () => {
        if (questionTag.classList.contains('open')) {
            questionTag.classList.remove('open');
        } else {
            const questionTagOpen = document.querySelectorAll('.open');

            questionTagOpen.forEach((questionTagOpen) => {
                questionTagOpen.classList.remove('open');
            });
            questionTag.classList.add('open');
        }
    });



    var arrowTag = document.querySelectorAll('img.arrow')

    questionTag.addEventListener('click', () => {
        arrowTag[0].classList.toggle('rotate'); // missing [0] added here
    });
});

The addEventListener function is applied to an event target. Thus, you cannot apply it to the NodeList, which is stored in your arrowTag:

enter image description here

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