-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
63 lines (47 loc) · 1.47 KB
/
script.js
File metadata and controls
63 lines (47 loc) · 1.47 KB
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
var button = document.getElementById("enter");
var input = document.getElementById("userinput");
var ul = document.querySelector("ul");
var b=document.getElementsByTagName("li")//for getting access to the list elemnts
var pos;
var i;
function inputLength() {
return input.value.length;
}
function addDel(pos){
var delbtn=document.createElement("button");
delbtn.innerHTML="DELETE";
b[pos].appendChild(delbtn);
delbtn.onclick=removeParent;}
for(i=0;i<b.length;i++)
{
addDel(i);
}//this wlll add delete button to the existing elements of the list
function createListElement() {
var li = document.createElement("li");
var delbtn=document.createElement("button");
delbtn.appendChild(document.createTextNode("DELETE"));
li.appendChild(document.createTextNode(input.value));
li.appendChild(delbtn);
ul.appendChild(li);
input.value = "";
delbtn.onclick=removeParent;
}//this will create the delte button for every new element in the list
function addListAfterClick() {
if (inputLength() > 0) {
createListElement();
}
}
function addListAfterKeypress(event) {
if (inputLength() > 0 && event.keyCode === 13) {
createListElement();
}
}
ul.onclick=function(event){
var target=event.target;
target.classList.toggle("done");
}//this function will cross of the list item when done
function removeParent(evt){
evt.target.parentNode.remove();
}//function to delete the node
button.addEventListener("click", addListAfterClick);
input.addEventListener("keypress", addListAfterKeypress);