Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

144
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda