i'm trying to insert values from input field into an array then store it in localstorage, but the problem is every time i insert a new value it replaces the old value, rather i want it to be added to the existing values . bear with me if it's a silly question, i am a newbie :)
<html>
<body>
<form>
<input id="textInput" type=text>
<input type=button onclick="myFunction()" value="Add Number"/>
</form>
<div id="output"><div>
<script>
function myFunction() {
var sports = ["soccer", "baseball"];
var newSport = document.getElementById('textInput').value;
sports.push(newSport)
window.localStorage.setItem("sportskey", sports);
document.getElementById('output').innerHTML = window.localStorage.getItem('sportskey');
}
</script>
</body>
</html>
localStorage. I think you need toJSON.stringify()andJSON.parse().myFunction()is ran, you re-create the arraysports. You only need to do that if it doesn't already exist inlocalStorage.