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

107
Views
How to add class to an element using javascript
<section id="menu">
            <div class="logo">
                <img src="images/logo.png" alt="" />
                <h2>Dynamic</h2>
            </div>

            <div class="items">
                <li><i class="fad fa-chart-pie-alt"></i><a href="#">Dashboard</a></li>
                <li><i class="fab fa-uikit"></i><a href="#">UI Elements</a></li>
                <li><i class="fas fa-th-large"></i><a href="#">Tables</a></li>
                <li><i class="fas fa-edit"></i><a href="#">Forms</a></li>
                <li><i class="fab fa-cc-visa"></i><a href="#">Cards</a></li>
                <li><i class="fas fa-hamburger"></i><a href="#">Modal</a></li>
                <li><i class="fas fa-chart-line"></i><a href="#">Blank</a></li>
            </div>
        </section>

const checkbox = document.getElementById("checkbox");

checkbox.addEventListener("change", () => {
    menu.classList.toggle("dark");
    document.body.classList.toggle("dark");
});

I tried using document.getElementById("h2").className += "dark";

but nothing is happening, what am i doing wrong

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

0

it should work like you wished but be aware of that it will find every H2 element and it will change them.

var elements = document.getElementsByTagName("H2");
for (var step = 0; step < elements.length; step++) {
  elements[step].classList.add("dark");
}
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!