CuddlyDaTurtle
CuddlyDaTurtle

Reputation: 17

How to display the value the local storage holds on the HTML page

I have 2 problems:

  1. When I load the HTML page, and I look in the "Debug" in inspect it continuously says that the Local Storage is undefined

  2. How to display the Local Storage value in the HTML page

JS code:

var loadLives = setInterval(showLives, 100);
// get lives from localstorage
var livesInStorage = window.localstorage.getItem('lives');
let lives;

if(livesInstorage) {
  //parse localstorage data to Int
  lives = parseInt(livesInstorage, 10);
}
else {
  // initial value (in this case: 3)
  lives = 3;
}

function loseLife() {
    if (lives > 1) {
        lives = --lives;

        // set changed value in localStorage
        window.localstorage.setItem('lives', lives);
    }
    else {
        location.href='fail.html';
    }
}

function showLives() {
    document.getElementById("lifeamount").innerHTML = window.localstorage.getItem('lives');
}


Upvotes: 0

Views: 61

Answers (1)

Karin C
Karin C

Reputation: 505

From the code you shared I can see that you set the variable 'lives' to 3 as initial value, but you didn't set it in the Local Storage there. You only set it in the Local Storage when you call 'loseLives', but I don't see a call to this function.

Also, you have a typo. You wrote localstorage instead of localStorage.

Upvotes: 1

Related Questions