Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

168
Visualizações
Keep added class from radio buttons on reload

I've been working at this code for hours and keep running into a variety of issues, so the number of times I have edited and tried different ways of coding this is a lot. Basically, I am trying to create a 3 (or more) color theme toggler using radio buttons. It's supposed to add a class, based on which one has been selected, to the body tag so that I can use css and variables to change the styling.

I've gotten it to actually add the class to the body tag, but when reloading, it won't keep the class. I keep reading about localStorage, so I've tried adding that, but I've never used it before, so I don't know if the several ways I have tried are just... wrong or if they just don't work with what I'm trying to do.

I can only use JavaScript and/or JQuery. Here is what I currently have.

HTML

<input name="theme" type="radio" aria-label="Light" id="light" value="light">
<label for="light">Light</label>

<input name="theme" type="radio" aria-label="Medium" id="medium" value="medium">
<label for="medium">Medium</label>

<input name="theme" type="radio" aria-label="Dark" id="dark" value="dark">
<label for="dark">Dark</label>

SCRIPT

jQuery(window).on("load", function() {
    $("#light").click(function(){
        $("body").removeClass("medium");
        $("body").removeClass("dark");
        $("body").addClass("light");
        localStorage.ClassName = "light";
    });

    $("#medium").click(function() {
        $("body").removeClass("light");
        $("body").removeClass("dark");
        $("body").addClass("medium");
        localStorage.ClassName = "medium";
    });

    $("#dark").click(function() {
        $("body").removeClass("light");
        $("body").removeClass("medium");
        $("body").addClass("dark");  
        localStorage.ClassName = "dark";
    });
});

I have also, to note, tried the individual sections (with their relevant class names) as

$("#dark").click(function() {
    $("body").removeClass("light");
    $("body").removeClass("medium");
    $("body").addClass(localStorage.getItem('dark'));
});
about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Your Problem is localStorage.getItem("dark") Should Be localStorage.getItem("ClassName")

And a best practice you should use method localStorage.setItem("ClassName", "light") to assign data

Here is a MDN article about LocalStorage.

This your code edited:

jQuery(window).on("load", function() {
    $("#light").click(function(){
        $("body").removeClass("medium");
        $("body").removeClass("dark");
        $("body").addClass("light");
        localStorage.setItem("ClassName", "light")
    });

    $("#medium").click(function() {
        $("body").removeClass("light");
        $("body").removeClass("dark");
        $("body").addClass("medium");
        localStorage.setItem("ClassName", "medium")
    });

    $("#dark").click(function() {
        $("body").removeClass("light");
        $("body").removeClass("medium");
        $("body").addClass("dark");  
        localStorage.setItem("ClassName", "dark")
    });
});

$("#dark").click(function() {
    $("body").removeClass("light");
    $("body").removeClass("medium");
    $("body").addClass(localStorage.getItem('ClassName'));
});
about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda