-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjavascript.js
More file actions
73 lines (62 loc) · 1.96 KB
/
javascript.js
File metadata and controls
73 lines (62 loc) · 1.96 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//Model
//function to load todo if list is found in local storage.
var list = document.querySelector('ul');
function loadTodo() {
if (localStorage.getItem('todoList')) {
list.innerHTML = localStorage.getItem('todoList');
}
}
//save todolist state so user can access it later
function saveElement() {
localStorage.setItem('todoList', list.innerHTML);
alert('Your TODO Saved!');
}
//function excluding execute script before the page loads
function onLoadChecked() {
list.addEventListener('click', function (event) {
if (event.target.tagName === 'LI') {
event.target.classList.toggle('checked');
} else if (event.target.tagName === 'SPAN') {
var div = event.target.parentNode;
div.remove();
}
}, false);
}
//View
function addElement() {
var li = document.createElement('li');
var inputValue = document.getElementById('toDoEl').value;
var text = document.createTextNode(inputValue);
li.appendChild(text);
if (inputValue == '') {
alert('Type your note');
} else {
document.getElementById('list').appendChild(li);
}
document.getElementById('toDoEl').value = '';
var span = document.createElement('SPAN');
var txt = document.createTextNode('X');
span.className = 'close';
span.appendChild(txt);
li.appendChild(span);
}
//Controller
var addButton = document.querySelector('.addBtn');
var saveButton = document.querySelector('.saveBtn');
//on page load loadTodo()
document.addEventListener('DOMContentLoaded', () => {
loadTodo();
onLoadChecked();
});
//on click button 'add' newElement()
addButton.addEventListener('click', () => {
addElement();
});
//on click button 'save' saveElement()
saveButton.addEventListener('click', () => {
saveElement();
});
// document.body.onload = () => {
// $C.initialize();
// }
module.exports = { addButton, saveButton };