-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
44 lines (38 loc) · 1.23 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
// SELECT DOM ELEMENTS
var input = document.getElementById("input"),
button = document.getElementById("enter"),
ul = document.querySelector("ul");
// TO CREATE A DELETE BUTTON & STYLE IT
function deleteButton () {
var button = document.createElement("button");
button.innerHTML = "delete";
button.classList.add("delete");
return button;
}
// PERFROMS 3 FUNCTIONS. ADDS/DELETE AN ITEM, ADDS DELETE BUTTON, AND ADDS THE "DONE" EVENT LISTENER
function toggleListItem () {
var li = document.createElement("li");
var del = deleteButton();
var val = document.createTextNode(input.value);
if (input.value){
li.appendChild(val);
li.appendChild(del);
ul.appendChild(li);
}
input.value = '';
del.addEventListener("click", function(){
ul.removeChild(li);
})
li.addEventListener("click", function(){
li.classList.toggle("done");
})
}
//ADD ELEMENT BY KEYPRESS
function addByKey (event) {
if (event.keyCode === 13) {
toggleListItem();
}
}
//CALL
button.addEventListener("click", toggleListItem);
input.addEventListener("keypress", addByKey);