forked from nutchaponhan/bootcamp-dom-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjs-script.js
60 lines (44 loc) · 1.42 KB
/
js-script.js
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
const addButton = document.getElementById('add');
const input = document.getElementById('userInput');
const ul = document.querySelector('ul');
const item = document.getElementsByTagName('li');
const ENTER_BUTTON_CODE = 13;
function inputLength() {
return input.value.length;
}
function listLength() {
return item.length;
}
function createListElement() {
const newLiElement = document.createElement('li');
const inputValue = input.value;
newLiElement.appendChild(document.createTextNode(inputValue));
ul.appendChild(newLiElement);
input.value = ''; //Reset text input field
function crossOut() {
newLiElement.classList.toggle('done');
}
// Add Click Event Listener to itself
newLiElement.addEventListener('click', crossOut);
// START ADD DELETE BUTTON
const deleteBtn = document.createElement('button');
deleteBtn.appendChild(document.createTextNode('X'));
newLiElement.appendChild(deleteBtn);
function deleteListItem() {
newLiElement.classList.add('delete');
}
// Add Click Event Listener to deleteBtn
deleteBtn.addEventListener('click', deleteListItem);
}
function addListAfterClick() {
if (inputLength() > 0) {
createListElement();
}
}
function addListAfterKeypress(event) {
if (inputLength() > 0 && event.which === ENTER_BUTTON_CODE) {
createListElement();
}
}
addButton.addEventListener('click', addListAfterClick);
input.addEventListener('keypress', addListAfterKeypress);