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

264
Vistas
Using localstorage to save background

I have a button which change background color and title color

button id="change" onclick="change()">Change background!</button>                                           

var state = true;



function change(){
         if (state){
        document.body.style.backgroundColor = "red";
        document.getElementById('title').style.color='blue';

         } else {
            document.body.style.backgroundColor = "#005fff";
            document.getElementById('title').style.color='lightgreen';
                  }
        state = !state;
}

I use state to choose between one and second option, if you have better solutions pls tell me.

I want to save the setting in localstorage(after refresh and quit i want to stay the background which was chosen).

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

U have to update the localStorage with ur own defined key, ichose bgcolor for that in the example.

Also when the script is loaded u should apply the bgcolor on the document body, asigning the value u get from the localStorage.

If no value was found, i set the background on white. this is optional of course :)

var state = true;

document.body.style.backgroundColor = localStorage.getItem('bgcolor') || 'white';

function change(){
  if (state){
    document.body.style.backgroundColor = "red";
    document.getElementById('title').style.color='blue';
    localStorage.setItem('bgcolor', 'red');

  } else {
    document.body.style.backgroundColor = "#005fff";
    document.getElementById('title').style.color='lightgreen';
    localStorage.setItem('bgcolor', '#005fff');
  }
  state = !state;
}
<h1 id="title">Title</h1>
<button id="change" onclick="change()">Change background!</button>

about 4 years ago · Juan Pablo Isaza Denunciar

0

You could store the current color in local storage like this:

function change(){
  const colorOption = localStorage.getItem('colorOption');
  if (colorOption === "2"){
    document.body.style.backgroundColor = "red";
    document.getElementById('title').style.color='blue';
    localStorage.setItem('colorOption', '1');
  } else {
    document.body.style.backgroundColor = "#005fff";
    document.getElementById('title').style.color='lightgreen';
    localStorage.setItem('colorOption', '2');
  }
}
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