-
Notifications
You must be signed in to change notification settings - Fork 0
/
todo.js
73 lines (61 loc) · 2.23 KB
/
todo.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
var ul = document.getElementById('list');
var li;
var addButton = document.getElementById('add');
addButton.addEventListener('click',addItem);
var removeButton = document.getElementById('remove');
removeButton.addEventListener('click',removeItem);
var removeAllButton = document.getElementById('removeAll');
removeAllButton.addEventListener('click',removeAllItems);
function addItem(){
var input = document.getElementById('input');
var item = input.value;
ul = document.getElementById('list');
var textnode = document.createTextNode(item);
if(item == ''){
// let p = document.createElement('p')
// let alertVal = document.createTextNode("Please enter some value")
// p.appendChild(alertVal)
// document.querySelector('#input').appendChild(p)
let x = document.getElementById('input')
x.insertAdjacentHTML('afterend','<p style="color:red">Please enter some value</p>')
return false;
}
else{
li = document.createElement('li');
//chechbox
var checkbox = document.createElement('input');
checkbox.type = 'checkbox';
checkbox.setAttribute('id','check');
//label
var label = document.createElement('label');
label.setAttribute('for','item');
// <ul id="list">
// <li class="mycheck"> <input type="checkbox" id="check"> <label>Complete the course</label> </li>
// <li class="mycheck"> <input type="checkbox" id="check" checked> <label>Do some exercise</label> </li>
// </ul>
//add these element on web page
ul.appendChild(label);
li.appendChild(checkbox);
label.appendChild(textnode);
li.appendChild(label);
ul.insertBefore(li,ul.childNodes[0]);
setTimeout(() => {
li.className = 'visual';
}, 3);
input.value ='';
return true
}
}
function removeItem(){
//console.log("Remove button clicked");
li = ul.children;
for (let index = 0; index < li.length; index++) {
while (li[index] && li[index].children[0].checked) {
ul.removeChild(li[index]);
}
}
}
function removeAllItems(){
li = ul.children;
ul.remove(li);
}