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

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

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 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