-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtodo.js
More file actions
45 lines (37 loc) · 1.19 KB
/
todo.js
File metadata and controls
45 lines (37 loc) · 1.19 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
var submitButton = document.querySelector('#submit-button');
var todoList = document.querySelector('.todo-item-list');
var inputText = document.querySelector("#todo-input");
var itemTotal = 0;
submitButton.addEventListener('click',function(){
itemTotal++;
//append item to bottom of list
//assign item unique id
var todoItem = inputText.value;
var newItem = document.createElement('li');
newItem.id = 'item-' + itemTotal;
newItem.innerHTML = todoItem;
//append checkbox to new item
//assign checkbox unique id
var checkBox = document.createElement('input');
checkBox.type = 'checkbox';
checkBox.id = 'checkbox-' + itemTotal;
checkBox.onclick = removeItem;
//verify that input value isnt blank
if (todoItem === ''){
//do nothing
} else {
newItem.appendChild(checkBox);
todoList.appendChild(newItem);
}
//clear input text
inputText.value = '';
});
function removeItem(){
var checkBoxId = this.id.replace("checkbox-", "");
var itemChecked = document.querySelector("#item-" + checkBoxId);
//adding strikethrough and removing item after 3 seconds
itemChecked.classList.add('checked-item');
setTimeout(function(){
itemChecked.remove();
}, 1000);
}