-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist_to_localStorage.js
More file actions
44 lines (38 loc) · 999 Bytes
/
Copy pathlist_to_localStorage.js
File metadata and controls
44 lines (38 loc) · 999 Bytes
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
document.addEventListener('DOMContentLoaded', () => {
let listState = [];
const but = document.querySelector('button');
const input = document.querySelector('input');
const ul = document.querySelector('ul');
const value = localStorage.getItem('listState');
if (value) {
listState = JSON.parse(value);
}
const store = (item) => {
listState.push(item);
localStorage.setItem('listState', JSON.stringify(listState));
};
const render = () => {
ul.innerHTML = '';
listState.forEach((text) => {
const li = document.createElement('li');
li.textContent = text;
ul.appendChild(li);
});
};
but.addEventListener('click', () => {
const text = input.value;
input.value = '';
store(text);
render();
});
input.addEventListener('keydown', (e) => {
if (e.key !== 'Enter' || input.value === '') {
return;
}
const text = input.value;
input.value = '';
store(text);
render();
});
render();
});