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

117
Views
Why does classList throw 'undefined' unless I specifically use getElementById?

My brain is melting for Why I can't select multiple elements with the same class (querySelector, querySelectorAll, getElementsByClassName, etc) and use classList to check if just one div contains a class.

I've seen tutorials written where it exactly uses getElementsbyClassName as examples, like they work. In my case, on Firefox, it's Uncaught TypeError: document.getElementsByClassName(...).classList is undefined

I'm giving these divs a css class ontoggle -

<div class="dot-container">
   <div class="dot-button" id="1" onclick="activateDot(this.id)"></div>
   <div class="dot-button" id="2" onclick="activateDot(this.id)"></div>
   <div class="dot-button" id="3" onclick="activateDot(this.id)"></div>
   <div class="dot-button" id="4" onclick="activateDot(this.id)"></div>
</div>


function activateDot(id) {
     if(document.getElementsByClassName('dot-button').classList.contains('db-active')) {
        console.log('bruh');
     } else {
        console.log('bruw');
     }
     document.getElementById(id).classList.toggle('db-active');
}

What worked was going -

function activateDot(id) {
   document.getElementById(1).classList.remove('db-active');
   document.getElementById(2).classList.remove('db-active');
   document.getElementById(3).classList.remove('db-active');
   document.getElementById(4).classList.remove('db-active');
   document.getElementById(id).classList.toggle('db-active');
}

Brrrr

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

getElementsByClassName returns a list of DOM nodes, while getting an element by ID returns a single element. You need to iterate over the list of elements and add / toggle the desired class for each one of them

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!