-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
69 lines (61 loc) · 2.08 KB
/
app.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
//! Adding input value to list
let form = document.querySelector("form");
let ul = document.querySelector("ul");
let del = document.querySelectorAll("#del");
let edt = document.querySelectorAll("#edt");
//! Alert on page reload
// window.addEventListener("beforeunload", function (e) {
// e.preventDefault(); // If you prevent default behavior in Mozilla Firefox prompt will always be shown
// e.returnValue = "";
// });
form.addEventListener("submit", e => {
e.preventDefault();
let val = e.target[1].value;
if (val === "") {
alert("Nothing to add please add something");
e.target[1].attributes.setAttribute("autofocus", "true");
} else {
let add = `<input type="text" value="${val}" disabled><button id="edt">Edit</button><button id="del">Delete</button>`;
let li = document.createElement("li");
ul.appendChild(li);
li.innerHTML = add;
let del = li.childNodes[2];
del.addEventListener("click", e => {
e.target.parentElement.remove();
});
let edt = li.childNodes[1];
edt.addEventListener("click", e => {
let tog = e.target.classList.toggle("active");
if (tog) {
e.target.previousElementSibling.removeAttribute("disabled");
e.target.style.background = "Green";
e.target.innerHTML = "✔";
} else {
e.target.previousElementSibling.setAttribute("disabled", "true");
e.target.style.background = "darkGoldenRod";
e.target.innerHTML = "✂";
}
});
}
});
//! deleting the element from list
del.forEach(val =>
val.addEventListener("click", e => {
e.target.parentElement.remove();
})
);
//! Editing the List items
edt.forEach(val =>
val.addEventListener("click", e => {
let tog = e.target.classList.toggle("active");
if (tog) {
e.target.previousElementSibling.removeAttribute("disabled");
e.target.style.background = "Green";
e.target.innerHTML = "✔";
} else {
e.target.previousElementSibling.setAttribute("disabled", "true");
e.target.style.background = "darkGoldenRod";
e.target.innerHTML = "✂";
}
})
);