Local Storage
Store simple information in the browser using localStorage.
What you should understand
Read the lesson carefully, study the example, then use the editor below to experiment with the code yourself.
<!DOCTYPE html>
<html>
<body>
<button onclick="saveName()">Save Name</button>
<button onclick="readName()">Read Name</button>
<p id="output"></p>
<script>
function saveName() {
localStorage.setItem("studentName", "Alex");
document.getElementById("output").textContent =
"Name saved!";
}
function readName() {
let name = localStorage.getItem("studentName");
document.getElementById("output").textContent =
name || "No name found";
}
</script>
</body>
</html>
Output
See what your code produces.
Test your understanding
Choose the best answer.
Which method can select an element by its ID?