-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocalStorage.html
61 lines (51 loc) · 2.05 KB
/
localStorage.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<html>
<body>
<form class="form">
<input class="input" placeholder="enter a value" name="inputVal">
<button name="save">Save</button>
<button name="get">Get Local Storage Values</button>
</form>
</body>
<script>
// go to application tab and go to local storage.
const form = document.querySelector('.form');
const store = [];
const get = document.querySelector('[name=get]');
form.addEventListener('submit', evt => {
evt.preventDefault();
console.log(evt.currentTarget.inputVal.value);
// return if empty.
if(!evt.currentTarget.inputVal.value) return;
const item = {
id: Date.now(),
value: evt.currentTarget.inputVal.value
};
store.push(item);
console.log(store);
// to clear the form ro reset.
evt.currentTarget.reset();
// save to Local Storage.
saveToLocalStorage();
});
function saveToLocalStorage() {
// step 1.
localStorage.setItem('item', store);
// local storage is text only.
// which means what we pass to local storage it will add .toString to it and store it.
// so what even the item would be maybe array,bool,object it will add .toString to it.
console.log(['a','b'].toString());
console.log('srk'.toString());
console.log({'name': 'srk'}.toString()); // it will return object object.
// step 2.
// so how do we convert an object to a string.
// JSON.
// the right way to pass an object to local storage.
localStorage.setItem('item', JSON.stringify(store));
}
function getValueFromLocalStorage() {
const values = JSON.parse(localStorage.getItem('item'));
console.log(values);
}
get.addEventListener('click', getValueFromLocalStorage);
</script>
</html>