-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
56 lines (46 loc) · 1.16 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
51
52
53
54
55
56
var button = document.querySelectorAll('button')[0];
var input = document.querySelectorAll('input')[0];
var ul = document.querySelectorAll('ul')[0];
function inputCheck() {
if (input.value.length > 0) {
return true
}
}
function newElement(){
var li = document.createElement('li');
var but = document.createElement('button');
li.appendChild(document.createTextNode(input.value));
but.appendChild(document.createTextNode('delete'));
ul.appendChild(li);
ul.appendChild(but);
input.value = '';
}
function addToListAfterClick(){
if (inputCheck()) {
newElement();
}
}
function addToListAfterEnter(event){
if (inputCheck() && event.keyCode === 13) {
newElement();
}
}
function toggleDoneStyle(element){
if (element.nodeName === 'LI'){
element.classList.toggle('done');
}
}
function deleteElement(element){
if (element.nodeName === 'BUTTON'){
element.previousSibling.remove();
element.remove()
}
}
function ulClick(event){
var element = event.target
toggleDoneStyle(element);
deleteElement(element);
}
button.addEventListener('click', addToListAfterClick);
input.addEventListener('keypress', addToListAfterEnter);
ul.addEventListener('click', ulClick);