1

This is my first go at localStorage and its not going. I have the jQuery code below to increase the page font size if an element is selected. Problem is when you go to a new page, it reverts back. I've tried some different variations but I am stuck. Any help is appreciated.

$textSize1.click(function() {
  $( "body" ).css("font-size", "10px");
  localStorage.setItem("$textSize1", $textSize1);
});
$textSize2.click(function() {
  $( "body" ).css("font-size", "20px");
  localStorage.setItem("$textSize2", $textSize2);
});
$textSize3.click(function() {
  $( "body" ).css("font-size", "30px");
  localStorage.setItem("$textSize3", $textSize3);
});

localStorage.getItem("$textSize1");
$textSize1.click(function() {
  $( "body" ).css("font-size", "10px");
  localStorage.setItem("$textSize1", $textSize1);
});
$textSize2.click(function() {
  $( "body" ).css("font-size", "20px");
  localStorage.setItem("$textSize2", $textSize2);
});
$textSize3.click(function() {
  $( "body" ).css("font-size", "30px");
  localStorage.setItem("$textSize3", $textSize3);
});

localStorage.getItem("$textSize1");
8
  • Why do you keep reassigning storeValue? What do you hope will be in that variable by the time you retrieve it? Commented Dec 10, 2015 at 17:51
  • localStorage isn't really for storing jQuery objects containing DOM elements Commented Dec 10, 2015 at 17:52
  • Where is the code that checks localStorage before you set it? You need to see what's in there before you can decide whether to overwrite it or not! Commented Dec 10, 2015 at 17:52
  • 1
    Localstorage is used to store string, you are trying to store a jq object?! And anyway, i really don't get the logic behind your code Commented Dec 10, 2015 at 17:52
  • Try "localStorage.getItem("$textSize1") to see if you actually get a value back. Not really sure what's going on with storeValue Commented Dec 10, 2015 at 17:54

1 Answer 1

3

What is happening is that you are storing the value and fixing the style in the current page but you are not doing the same operation when the page load again.

This is normally done in the onReady written in jQuery like this : $(document).ready(function() {...});

Just reading the value won't do much. You need to use it!

I think I've captured what you were trying to do and reimplemented your code:

var defaultFontSize = "10px";

$(document).ready(function(){
    var fontSize = localStorage.getItem("fontSize") || defaultFontSize;
    $( "body" ).css("font-size", fontSize);
})

$fontSize1.click(function() {
  $("body").css("font-size", "10px");
  localStorage.setItem("fontSize", "10px");
});
$fontSize2.click(function() {
  $("body").css("font-size", "20px");
  localStorage.setItem("fontSize", "20px");
});
$fontSize3.click(function() {
  $("body").css("font-size", "30px");
  localStorage.setItem("fontSize", "30px");
});
Sign up to request clarification or add additional context in comments.

2 Comments

ive played around with it for 20 mins. i think i'll save the selection to the db instead. thanks all!
My personal opinion is that unless you want that setting to be long-lived across computers for the same user, localStorage is enough. The code I provided should make that work quite easily.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.