admin管理员组文章数量:1357702
im trying to set x's age to the local storage item 'age' however, for a reason i do not know, this will not work.
Here is my code:
var x = {
age: 37,
gender: "male",
ine: 17000,
};
localStorage.setItem("age") = x.age;
alert(localStorage.getItem('age'));
im trying to set x's age to the local storage item 'age' however, for a reason i do not know, this will not work.
Here is my code:
var x = {
age: 37,
gender: "male",
ine: 17000,
};
localStorage.setItem("age") = x.age;
alert(localStorage.getItem('age'));
Share
Improve this question
asked Mar 26, 2016 at 16:00
j. holj. hol
311 gold badge1 silver badge3 bronze badges
2 Answers
Reset to default 5The problem is with your syntax. You need to use it this way.
localStorage.setItem("age", x.age)
alert(localStorage.getItem('age'));
The idea is simple. You are storing the data against a name. And then retrieve it using the same name.
Open the Developer Tools in your browser. Look at the console.
Uncaught TypeError: Failed to execute 'setItem' on 'Storage': 2 arguments required, but only 1 present.
Then look at the manual which says:
storage.setItem(keyName, keyValue);
Then get your syntax correct:
var x = {
age: 37,
gender: "male",
ine: 17000,
};
localStorage.setItem("age", x.age);
alert(localStorage.getItem('age'));
本文标签: javascriptlocal storage not accepting value from objectStack Overflow
版权声明:本文标题:javascript - local storage not accepting value from object - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744080710a2587585.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论