Okay, so I'm lost here. I've tried all (or so it seems). I'm brand new to javascript and I'm trying to save inputdata into an array and then to localstorage. It works fine until I refresh the page, then the array is empty and the data is lost.
Can someone help?
Here's my code. Very simpel actually.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<input id="input-id-first-name" value="Frontname">
<button id="btn">Send value</button>
<p id="output"></p>
</body>
<script>
const inputFirstNameEl = document.querySelector("#input-id-first-name");
const btnEl = document.querySelector("#btn");
const outputEl = document.querySelector("#output");
const array = []
let getStorage = JSON.parse(localStorage.getItem("storage"))
btnEl.addEventListener("click", getInput);
function getInput () {
localStorage.setItem("storage", JSON.stringify(array));
const getInputFirst = inputFirstNameEl.value;
array.push(getInputFirst);
}
console.log(JSON.parse(localStorage.getItem("storage")));
</script>
</html>