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

188
Views
Having trouble changing classes to diferent tags of html with js and css

I was trying to do the same thing to body but for other tags like button, fieldset, etc

The way to change the body tag for darkmode is "document.body.classList.add("darkmode");" but i need to know the same thing but for other tags like the ones i named before.

My JS code

let darkMode = localStorage.getItem("darkMode");

const enableDarkMode = () => {
    document.body.classList.add("darkmode");
    localStorage.setItem("darkMode", "enable");
}

const disableDarkMode = () => {
    document.body.classList.remove("darkmode");
    localStorage.setItem("darkMode", null);
}

if(darkMode === "enable"){
    enableDarkMode()
}

$("#dark_mode_toggle").click(() => { 
    darkMode = localStorage.getItem("darkMode");
    if (darkMode !== "enable"){
        enableDarkMode();
    }else{
        disableDarkMode();
    }
});
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Kinglish's suggestion in the comments does seem right, after all CSS stands for Cascading StyleSheets, but here is a JS solution to what you were asking for.

Since it seems like you are using JQuery, you can do this:

$("button").addClass("darkmode");

Without JQuery, you can do this:

document.querySelectorAll("button").forEach(element => element.classList.add("darkmode"));

For other elements, like fieldset, just repeat the code you chose to use from above. I think it should be trivial to figure out how to disable the dark mode using the code you already have and this.

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!