Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Reading a sessionStorage property and creating an if statement

So, I want to read a value from sessionStorage. I know how to do this, but then I want to do something like this:

function whiteSet() {
    if (// condition: what the heck would I put here?) {
        const color = localStorage.getItem('theme');
        //if the value  'theme' is equal to 'aluminum' then:
        document.getElementById("headerbar").style.backgroundImage = "url(images/themes/aluminum.png)";
    } else {
        document.getElementById("headerbar").style.backgroundImage = "url(images/themes/galaxy.png)";
    };

I don’t really know what to do :/
If an answer already exists for this question, I have already tried finding it and failed.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Just take it step by step. First you’re reading the value out of local storage and putting it in the variable ‘color’, then you need to decide what to do based on what value you read out. So instead of starting out with an if statement, get color first and then the conditional tests against its content:

function whiteSet() {
  const theme = localStorage.getItem('theme');
  if (theme === "aluminum") {
    document.getElementById("headerbar").style.backgroundImage = "url(images/themes/aluminum.png)";
  } else {
    document.getElementById("headerbar").style.backgroundImage = "url(images/themes/galaxy.png)";
  };
}

(Personally, since you’re storing it in a localStorage key named "theme", I’d name the variable "theme" too instead of "color" just to avoid confusion; but that’s more of a coding style thing than a requirement for it to work.)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading