admin管理员组文章数量:1426643
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">×</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">×</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:
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 badgesindex.html Uncaught TypeError: Failed to execute 'setItem' on 'Storage': 2 arguments required, but only 1 present.
2 Answers
Reset to default 5When 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"))
本文标签: javascriptMy localStorage is not saving anythingStack Overflow
版权声明:本文标题:javascript - My localStorage is not saving anything - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745464413a2659469.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论