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
Add a ment  | 

2 Answers 2

Reset to default 5

The 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