-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
43 lines (28 loc) · 1.07 KB
/
script.js
File metadata and controls
43 lines (28 loc) · 1.07 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
window.addEventListener("load", function(){
// get the name of the user and store it in a variable
const nameInput = document.getElementById("firstName");
const todoForm = document.getElementById("create_todo");
todoList = JSON.parse(localStorage.getItem('todoList')) || [];
// check in the localstorage if there is a name already
const userName = localStorage.getItem("user") ||"";
nameInput.value = userName;
nameInput.addEventListener("change", function(e) {
localStorage.setItem("user", e.target.value);
})
todoForm.addEventListener('submit', (e) => {
e.preventDefault();
const userTodo={
content: e.target.elements.content.value,
category: e.target.elements.category.value,
createdAt: new Date().getTime(),
done: false,
}
/*if(!userTodo.content){
alert("Field cannot be empty.please fill in a todo")
} else{
todoList.push(userTodo)
}*/
!userTodo.content ? alert("field cannot be empty.please fill in a todo") : todoList.push(userTodo)
localStorage.setItem('todoList', JSON.stringify(todoList))
})
})