-
Notifications
You must be signed in to change notification settings - Fork 77
/
todo2.js
106 lines (83 loc) · 3.02 KB
/
todo2.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
95
96
97
98
99
100
101
102
103
104
105
function getDate() {
//gives us the date in a format we like
return new Date().toDateString() + " " + new Date().toLocaleTimeString();
}
let store = localStorage.getItem('todos');
if(!store) {
localStorage.setItem('todos', JSON.stringify([]));
}
//once the webpage loads, we want to render all the todos in our todoArray
window.addEventListener('load', () => {
const todosFromStorage = localStorage.getItem('todos');
const todos = JSON.parse(todosFromStorage);
for (let obj of todos) {
createTodo(obj.content);
}
})
//select existing div in html
const todoParent = document.querySelector('.todos');
function createTodo(content) {
const todosFromStorage = localStorage.getItem('todos');
const todos = JSON.parse(todosFromStorage);
todos.push({ content: content });
localStorage.setItem('todos', JSON.stringify(todos));
//create an empty div
const todo = document.createElement('div');
//give that div a class of todo
todo.setAttribute('class', 'todo');
//create an empty paragraph
const contentParagraph = document.createElement('p');
//give the paragraph a class of content
contentParagraph.setAttribute('class', 'content')
//put the content from the object inside the paragraph
contentParagraph.innerText = content;
//create another empty paragraph
const dateParagraph = document.createElement('p');
//give the paragraph a class of date
dateParagraph.setAttribute('class', 'date')
//put the value coming from getDate function inside the paragraph
dateParagraph.innerText = getDate();
//add the 2 paragraphs created to the div we created
todo.appendChild(contentParagraph);
todo.appendChild(dateParagraph);
//add todo we created to existing element with class of todos
todoParent.appendChild(todo);
}
const addBtn = document.querySelector('.createTask');
const inputField = document.querySelector('#createTodoField');
addBtn.addEventListener('click', (e) => {
//prevent the page from reloading
//default behaviour of buttons is to reload page when clicked
//and we dont want that
e.preventDefault();
//if inputfield is empty
if(inputField.value.trim() === '') {
//alert
const p = document.querySelector('.white');
p.innerText = 'Input field must not be empty'
alert('Input field must not be empty');
inputField.value = '';
//return to stop the lines of code below from executing
return;
}
//call the createTodo function with whatever is in the input field
createTodo(inputField.value);
//reset the input field to an empty string after creating
inputField.value = '';
})
inputField.addEventListener('keypress', (e) => {
if(e.keyCode != 13) return;
if(inputField.value.trim() === '') {
//alert
const p = document.querySelector('.white');
p.innerText = 'Input field must not be empty'
alert('Input field must not be empty');
inputField.value = '';
//return to stop the lines of code below from executing
return;
}
//call the createTodo function with whatever is in the input field
createTodo(inputField.value);
//reset the input field to an empty string after creating
inputField.value = '';
})