-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
136 lines (103 loc) · 3.74 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// document.getElementsByTagName returns an HTML
// collection which is in the form of an array.
//document.getElementsByClassName also returns an
//HTML collection.
//getElementById method returns a value
// So the previous two methods result in the return of
//HTML collection and the getElementById returns
//a single value.
// document.querySelector
// The first instance of the element is selected
//with this command
// document.querySelectorAll
//Collection of the document elements is called as
//HTML collection.
//collection of document nodes(element, attributes and text)
//is a NodeList
//HTML collection can be accessed via an Id, name or
//index number. This is also a live collection.
//NodeList can only be accessed by the index number.
//It is a static collection in that the addition
//of an element via DOM is not immediately reflected
//in the NodeList
//The childnode property returns a live NodeList
//Several of the methods that are avaiable to the
//array isn't available for NodeList. It is reiterated
// time and again that NodeList may seem as an array in
// all likelihood but that is not the case.
// var button = document.getElementsByTagName("button")[0];
// button.addEventListener("mouseover",function () {
// console.log("Button clicked !!!");
// });
var button = document.getElementById("enter");
var input = document.getElementById("userinput");
// var ul = document.querySelector("ul");
var ol = document.querySelector("ol");
// var listItem = document.querySelectorAll("li");
//Caching the values in a variable
function inputLength(){
return input.value.length;
}
function createListElement() {
var li = document.createElement("li");
li.appendChild(document.createTextNode(input.value));
ol.appendChild(li);
input.value = "";
// li.setAttribute("class","list-style:none");
var del = document.createElement("button");
del.appendChild(document.createTextNode("Delete"));
ol.appendChild(del);
del.setAttribute("class","btn btn-danger");
function toggle(){
li.classList.toggle("done");
}
li.addEventListener("click",toggle);
// if(li.addEventListener("click",function toggle(){
// li.classList.toggle("done");
// }));
function deleteItem(){
li.remove();
del.remove();
}
del.addEventListener("click",deleteItem);
}
function keyPress(event) {
{
if(inputLength() > 0 && event.key === 'Enter')
{
createListElement();
}
else if(inputLength() <= 0 && event.key === 'Enter')
{
alert("Nothing Entered");
}
}
}
function buttonClick() {
if(inputLength() > 0)
{
createListElement();
}
else{
alert("Nothing entered");
}
}
// function toggleList(){
// listItem.classList.toggle("done");
// }
// function checkList(){
// for (var index = 0; index < listItem.length; index++) {
// listItem[index].addEventListener("click",function(){
// listItem[index].classList.toggle("done");
// });
// }
// }
//we are passing the functions as arguments in the form of reference but not calling them directly instead.
//That code calls back the function when a certain event takes place or a condition gets satisfied.
//So the functions passed in as argument to another function just as below is technically called "a callback
//function";
//Event for button click
button.addEventListener("click",buttonClick);
//Event for keypress
input.addEventListener("keypress",keyPress);
// listItem.forEach(checkList);