-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscripts.js
94 lines (71 loc) · 2.26 KB
/
scripts.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
function timeNow() {
const date = new Date();
const dateDiv = document.getElementById("date");
let hours = date.getHours();
let minutes = date.getMinutes();
let seconds = date.getSeconds();
hours = addZero(hours);
minutes = addZero(minutes);
seconds = addZero(seconds);
const current = hours + ":" + minutes + ":" + seconds;
dateDiv.textContent = current;
setInterval(timeNow, 1000);
}
function addZero(i) {
if(i < 10) {
i = "0" + i;
}
return i;
}
function dayNow() {
const date = new Date();
const today = date.getDay();
const days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"];
const h1 = document.getElementById("today");
h1.textContent = "Today is " + days[today];
}
window.addEventListener("load", timeNow());
window.addEventListener("load", dayNow());
const list = document.querySelector('ul');
list.addEventListener('click', function(e) {
if(e.target.tagName === 'LI') {
e.target.classList.toggle('checked');
}
}, false);
function addItem() {
const createLi = document.createElement('li');
let task = document.getElementById('task').value;
if (task === '') {
alert("You can't add an empty task!");
}
else {
createLi.textContent = task;
document.getElementById('list').appendChild(createLi);
}
document.getElementById('task').value = '';
const li = document.getElementsByTagName('li');
for (let i=0; i < li.length; i++) {
const span = document.createElement('span');
const x = document.createTextNode('\u00D7');
span.className = 'close';
span.appendChild(x);
li[i].appendChild(span);
}
const close = document.getElementsByClassName('close');
for (let i=0; i < close.length; i++) {
close[i].addEventListener('click', function() {
const parent = this.parentElement;
parent.style.display = 'none';
});
}
}
//task can be added by pressing enter
const button = document.getElementById('addTask');
let input = document.getElementById('task');
input.addEventListener("keydown", (e) => {
if(e.keyCode === 13) {
e.preventDefault();
button.click();
}
})
button.addEventListener('click', addItem);