Reputation: 201
I have a button that when a div is clicked a text is printed on another div. I want this text to be saved to local storage. I've tried this:
$(".light.theme").click(function(){
$('.current-theme').html("Light is active");
localStorage.content = $('.current-theme').html();
$('.current-theme').html(localStorage.content);
});
But its not working. Can you help?
This is my "virgin" code:
<div class="current-theme">Default is active</div>
<div class="light theme">Light</div>
$(".light.theme").click(function(){
$(".current-theme").html("Light is active");
});
Upvotes: 0
Views: 423
Reputation: 977
To save to localStorage you need to write like this.
window.localStorage.setItem('key', 'value')
So in your case you need to do this
<div class="current-theme">Default is active</div>
<div class="light theme">Light</div>
$(document).ready(function(){
$(".light.theme").click(function(){
$(".current-theme").html("Light is active");
window.localStorage.setItem('theme', "Light")
});
var theme = window.localStorage.getItem('theme');
if(theme && theme != '') {
$(".current-theme").html(theme+" is active");
}
})
And to get back the saved value from local storage you need to do this.
window.localStorage.getItem('theme')
Upvotes: 1