javascript - My localStorage is not saving anything - Stack Overflow

So this is like a ToDo list site, but when I refresh nothing is saved!Here are the HTML and Javascript

So this is like a ToDo list site, but when I refresh nothing is saved!
Here are the HTML and Javascript:

localStorage.setItem('myInput'); {
  var save = localStorage.getItem("myInput");
}
localStorage.setItem('myInput2'); {
  var save = localStorage.getItem("myInput2");
}
localStorage.setItem('myInput3'); {
  var save = localStorage.getItem("myInput3");
}
<div class="modal-content">
  <span class="close">&times;</span>
  <div id="myDIV" class="header">
    <h2>Add a task</h2>
    <input type="text" id="myInput" placeholder="Write a task...">
    <input type="text" id="myInput2" placeholder="Estimated time...">
    <input type="text" id="myInput3" placeholder="Week...">
    <span onclick="newElement()" class="addBtn" value="save" onClick="save()">Add</span>
  </div>
</div>
</div>


<ul id="tehtavat">
</ul>

So this is like a ToDo list site, but when I refresh nothing is saved!
Here are the HTML and Javascript:

localStorage.setItem('myInput'); {
  var save = localStorage.getItem("myInput");
}
localStorage.setItem('myInput2'); {
  var save = localStorage.getItem("myInput2");
}
localStorage.setItem('myInput3'); {
  var save = localStorage.getItem("myInput3");
}
<div class="modal-content">
  <span class="close">&times;</span>
  <div id="myDIV" class="header">
    <h2>Add a task</h2>
    <input type="text" id="myInput" placeholder="Write a task...">
    <input type="text" id="myInput2" placeholder="Estimated time...">
    <input type="text" id="myInput3" placeholder="Week...">
    <span onclick="newElement()" class="addBtn" value="save" onClick="save()">Add</span>
  </div>
</div>
</div>


<ul id="tehtavat">
</ul>

I also get this error in the console:

index.html Uncaught TypeError: Failed to execute 'setItem' on 'Storage': 2 arguments required, but only 1 present.

Share Improve this question edited Mar 5, 2018 at 10:57 Vadim Kotov 8,2848 gold badges50 silver badges63 bronze badges asked Dec 5, 2017 at 19:45 JokamuttaJokamutta 11 gold badge1 silver badge7 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

When you want to save value to localstorage you need to pass a value as second attribute. First one is the key.

localStorage.setItem('myInput', myValue);

Think of localstorage as list of key / value pairs.

Besides, you want to write data to localstorage manually each time your data changes, so let's say on keyup event:

document.getElementById('myInput').addEventListener('keyup', function(event){
    localStorage.setItem('inputValue', event.target.value);
})

And i can imagine that on load, you want to bring this value back;

var inputValue = localStorage.getItem('inputValue');

if(inputValue) {  //just check if there's value saved
    document.getElementById('myInput').value = inputValue;
}

Please take into consideration that i typed code out of my head without testing it, so there are some typos possible. You can as well maybe try to solve typos on your own.

Want to store string? Store like this:

  • localStorage.setItem("myInput", "valueFromInput")

Get like this:

  • localStorage.getItem("myInput")

Want to store array or object? Store like this :

  • Note : localStorage can only store strings so we need convert array into a string before storing. Convert looks like this :
     var data = JSON.stringify('{myData: "myData"}');
     localStorage.setItem("myInput", data)
    

Get data like this:

  • var data = JSON.parse(localStorage.getItem("myInput"))

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745464413a4628853.html

相关推荐

  • javascript - My localStorage is not saving anything - Stack Overflow

    So this is like a ToDo list site, but when I refresh nothing is saved!Here are the HTML and Javascript

    3小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信