Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

138
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!