how to save a jquery action in the localstorage?

0

It happens that I have a button that when I click on it, it changes the background to another color and I would like it to be saved in localStorage . So, every time you enter the web, be the color you chose. Let me explain:

function temas(){
$('#tema1').click(function(){
    $('body').css('background','#212121');
});

}

    
asked by Cesar 21.08.2016 в 21:59
source

3 answers

1

Well, it was only complementary with edgardo001

function temas(){ 
  $('#tema1').click(function(){
    $('body').css('background','#212121');

    //Guardalo
    window.localStorage.setItem('background', '#212121');
  });
}

and already in another part you recover it

if( window.localStorage.getItem('background') !== undefined){
    $('body').css('background', window.localStorage.getItem('background') );
}

and it's all:)

====

link

    
answered by 22.08.2016 / 16:54
source
2

I think this can help you:

//Configura un localStorage  
 localStorage.setItem("Nombre", "tuNombre");
 //Obtiene un localStage   
 var nombre = localStorage.getItem("Nombre");
    
answered by 21.08.2016 в 23:14
0

I do not know what you mean by a localstage ... but you could use cookies to store the color on the client side ... I leave you a link that could be useful .... link

//Para acceder a una cookie
var texto = $.cookie('nombre_cookie');
//Para setear una cookie
$.cookie('nombre_cookie','valor');
    
answered by 21.08.2016 в 23:01