Skip to content
This repository was archived by the owner on Feb 13, 2022. It is now read-only.

Commit f406924

Browse files
committed
solution wesbos#15
1 parent 9d21703 commit f406924

File tree

1 file changed

+44
-1
lines changed

1 file changed

+44
-1
lines changed

15 - LocalStorage/index-START.html

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,50 @@ <h2>LOCAL TAPAS</h2>
2828
<script>
2929
const addItems = document.querySelector('.add-items');
3030
const itemsList = document.querySelector('.plates');
31-
const items = [];
31+
const items = JSON.parse(localStorage.getItem('items')) || [];
32+
33+
function addItem(evt) {
34+
evt.preventDefault();
35+
36+
const text = this.querySelector('[name=item]').value;
37+
const item = {
38+
text,
39+
done: false
40+
};
41+
42+
items.push(item);
43+
populateList(items, itemsList);
44+
localStorage.setItem('items', JSON.stringify(items));
45+
46+
this.reset();
47+
}
48+
49+
function populateList(plates = [], platesList) {
50+
platesList.innerHTML = plates.map((plate, i) => {
51+
return `
52+
<li>
53+
<input id="item-${i}" type="checkbox" data-index=${i} ${plate.done ? "checked" : ''} />
54+
<label for="item-${i}">${plate.text}</label>
55+
</li>
56+
`;
57+
}).join('');
58+
}
59+
60+
function toggleDone(evt) {
61+
if (!evt.target.matches('input')) return;
62+
63+
const el = evt.target;
64+
const index = el.dataset.index;
65+
66+
items[index].done = !items[index].done;
67+
localStorage.setItem('items', JSON.stringify(items));
68+
populateList(items, itemsList);
69+
}
70+
71+
addItems.addEventListener('submit', addItem);
72+
itemsList.addEventListener('click', toggleDone);
73+
74+
populateList(items, itemsList);
3275

3376
</script>
3477

0 commit comments

Comments
 (0)