-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
58 lines (54 loc) · 1.75 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
// Add Close btn to each to do list element
let myLi=document.querySelectorAll("li");
for(var i=0 ; i<myLi.length;i++){
let span=document.createElement("span");
let text=document.createTextNode("\u00D7");
span.className="close";
span.appendChild(text);
myLi.appendChild(span);
}
// click on close btn to hide the list item
let close=document.getElementsByClassName("close");
for(var i=0;i<close.length;i++){
close[i].onclick=function(){
let div=this.parentElement;
div.style.display="none";
}
}
// Add checked when click on an list element
let list = document.querySelector('ul');
list.addEventListener('click',function(ev){
if(ev.target.tagName==='LI'){
ev.target.classList.toggle('checked');
}
},false);
// create a list item when clicking on the add btn
function newElement() {
var li = document.createElement("li");
var inputValue = document.getElementById("myInput").value;
var t = document.createTextNode(inputValue);
li.appendChild(t);
if (inputValue === '') {
alert("You must write something!");
} else {
document.getElementById("myUL").appendChild(li);
}
document.getElementById("myInput").value = "";
var span = document.createElement("SPAN");
var txt = document.createTextNode("\u00D7");
span.className = "close";
span.appendChild(txt);
li.appendChild(span);
for (i = 0; i < close.length; i++) {
close[i].onclick = function() {
var div = this.parentElement;
div.style.display = "none";
}
}
}
// create a list item when hit enter
document.addEventListener("keydown", function (event) {
if (event.keyCode === 13) {
newElement();
}
});