-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
50 lines (42 loc) · 1.25 KB
/
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
'use strict';
let button = document.getElementById('enter');
let input = document.getElementById('userinput');
let ul = document.querySelector('ul');
function inputLength() {
return input.value.length;
}
function createListElement() {
let li = document.createElement('li');
li.appendChild(document.createTextNode(input.value));
ul.appendChild(li);
li.addEventListener('click', function () {
let finished = this.classList.toggle('done');
let removeButton = document.createElement('button');
removeButton.classList.add('deleteButton');
if (finished) {
removeButton.appendChild(
document.createTextNode('Remove')
);
removeButton.classList = 'deleteButton';
li.appendChild(removeButton);
removeButton.addEventListener('click', function () {
this.parentElement.remove();
});
} else {
this.getElementsByClassName('deleteButton')[0].remove();
}
});
input.value = '';
}
function addListAfterClick() {
if (inputLength() > 0) {
createListElement();
}
}
function addListAfterPress(event) {
if (inputLength() > 0 && event.keyCode === 13) {
createListElement();
}
}
button.addEventListener('click', addListAfterClick);
input.addEventListener('keypress', addListAfterPress);