-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
69 lines (57 loc) · 1.54 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
57
58
59
60
61
62
63
64
65
66
67
var userInput = document.getElementById("textbox");
var enterBtn = document.getElementById("enter");
var ul = document.querySelector("ul");
var li = document.getElementsByTagName("li")
enterBtn.addEventListener("click", addListAfterClick);
userInput.addEventListener("keypress", addListAfterEnter);
getListElements();
getButtonListElement();
function addListAfterClick() {
//console.log("Good Job you click");
if (inputLength() > 0) {
createListItem();
}
}
function addListAfterEnter(event) {
if (inputLength() > 0 && event.keyCode === 13) {
createListItem();
}
//console.log(event);
}
//Create new list items to shopping list
function createListItem() {
var li = document.createElement("li");
var button = document.createElement("button");
li.appendChild(document.createTextNode(userInput.value));
ul.appendChild(li);
li.appendChild(button);
button.innerHTML = "X";
userInput.value = "";
// Get new locations for li elements
getListElements();
getButtonListElement();
}
function toggleListElement() {
this.classList.toggle("done");
}
function inputLength() {
return userInput.value.length;
}
//Get list element location
function getListElements() {
for (i = 0; i < li.length; i++) {
li[i].addEventListener('click', toggleListElement)
}
}
//Get button element id
function getButtonListElement() {
var delBtn = document.querySelectorAll('li button');
for (i = 0; i < delBtn.length; i++) {
delBtn[i].addEventListener('click', clearListItem)
}
}
function clearListItem() {
for (i = 0; i < li.length; i++) {
this.parentNode.remove()
}
}