-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathscript.js
More file actions
86 lines (70 loc) · 2.41 KB
/
script.js
File metadata and controls
86 lines (70 loc) · 2.41 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
const localStorage = window.localStorage;
const addButton = document.getElementById('add-button');
const inputField = document.getElementById('input-field');
const itemsList = document.getElementById('items-list');
const clearButton = document.getElementById('clear');
const clearItems = () => {
localStorage.clear();
showItems();
}
const addItemToDOM = (currentItem, currentNum) => {
let li = document.createElement('li');
let checkbox = document.createElement('input');
checkbox.setAttribute('type', 'checkbox');
let span = document.createElement('span');
let text;
if (currentItem.includes('-completed')) {
let numTimesCompleted = currentItem.split('-completed').length - 1;
// if -completed appears more than once
if (numTimesCompleted > 1) {
text = document.createTextNode(currentItem.slice(0, -10 * numTimesCompleted));
localStorage.setItem('item ' + currentNum, currentItem.slice(0, -10 * (numTimesCompleted - 1)));
} else { // if -completed appears once
text = document.createTextNode(currentItem.slice(0, -10));
}
span.appendChild(text);
span.style.textDecoration = 'line-through';
} else {
text = document.createTextNode(currentItem);
span.appendChild(text);
}
checkbox.addEventListener('change', () => {
let previousText = localStorage.getItem('item ' + currentNum);
if (previousText.slice(-10, previousText.length) === '-completed') {
localStorage.setItem('item ' + currentNum, previousText.slice(0, -10));
}
localStorage.setItem('item ' + currentNum, previousText + '-completed');
span.style.textDecoration = 'line-through';
})
li.appendChild(checkbox);
li.appendChild(span);
itemsList.appendChild(li);
}
const showItems = () => {
itemsList.textContent = ''; // removes existing list items so items don't appear twice
let currentNum = 1;
while (true) {
let currentItem = localStorage.getItem('item ' + currentNum);
if (!currentItem) break;
addItemToDOM(currentItem, currentNum);
currentNum++;
}
}
const addItem = () => {
let numItems = localStorage.length;
let inputText = inputField.value;
if (inputText.length === 0) {
return
}
inputField.value = "";
localStorage.setItem('item ' + (numItems + 1), inputText);
showItems();
}
const init = () => {
showItems();
addButton.addEventListener('click', addItem);
clearButton.addEventListener('click', clearItems);
}
init();
// if check, get sibling and mark that as completed in local storage
// cross out the letters