Skip to content

Commit fb983d3

Browse files
committed
finished JS Day wesbos#15
1 parent d02a5d6 commit fb983d3

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

15 - LocalStorage/index-FINISHED.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ <h2>LOCAL TAPAS</h2>
5656
}
5757

5858
function toggleDone(e) {
59+
console.log(e.target)
5960
if (!e.target.matches('input')) return; // skip this unless it's an input
6061
const el = e.target;
6162
const index = el.dataset.index;
@@ -74,4 +75,3 @@ <h2>LOCAL TAPAS</h2>
7475

7576
</body>
7677
</html>
77-

15 - LocalStorage/index-START.html

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,43 @@ <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 handleSubmission(e){
34+
e.preventDefault();
35+
const input = e.target.children[0].value;
36+
const item = {input: input, done: false}
37+
items.push(item)
38+
localStorage.setItem('items', JSON.stringify(items))
39+
populateList()
40+
this.reset();
41+
}
42+
43+
function populateList(){
44+
const newItems = items.map((item, i) => {
45+
return `<li>
46+
<input type="checkbox" data-index=${i} id="item${i}" ${item.done ? 'checked' : ''} />
47+
<label for="item${i}">${item.input}</label>
48+
</li>`
49+
}).join('')
50+
itemsList.innerHTML = newItems;
51+
}
52+
53+
function toggleDone(e) {
54+
if (!e.target.matches('input')) return; // skip this unless it's an input
55+
const index = e.target.dataset.index
56+
items[index].done = !items[index].done;
57+
localStorage.setItem('items', JSON.stringify(items))
58+
}
59+
60+
61+
populateList()
62+
63+
addItems.addEventListener('submit', handleSubmission);
64+
itemsList.addEventListener('click', toggleDone);
3265

3366
</script>
3467

3568

3669
</body>
3770
</html>
38-

0 commit comments

Comments
 (0)