-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
52 lines (42 loc) · 1.39 KB
/
app.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
document.getElementById("formTask").addEventListener("submit", saveTask);
function saveTask(evento) {
let title = document.getElementById("title").value;
let description = document.getElementById("description").value;
const task = {
title, //title: title,
description, //description : description
id: new Date().valueOf().toString()
};
if (localStorage.getItem("tasks") === null) {
let tasks = [];
tasks.push(task);
localStorage.setItem("tasks", JSON.stringify(tasks));
} else {
let tasks = JSON.parse(localStorage.getItem("tasks"));
tasks.push(task);
localStorage.setItem("tasks", JSON.stringify(tasks));
}
getTasks();
document.getElementById('formTask').reset()
evento.preventDefault();
}
function getTasks() {
let tasks = JSON.parse(localStorage.getItem("tasks"));
let tasksView = document.getElementById("tasks");
tasksView.innerHTML = "";
tasks.forEach((task) => {
tasksView.innerHTML += `<div class="card mb-3">
<div class="card-body">
<p>${task.title} - ${task.description}</p>
<a class="btn btn-danger" onclick="deleteTask('${task.id}')">Delete</a>
</div>
</div>`;
});
}
function deleteTask(id) {
let tasks = JSON.parse(localStorage.getItem("tasks"));
tasks = tasks.filter( (task => task.id == id? false : true ) )
localStorage.setItem('tasks', JSON.stringify(tasks))
getTasks()
}
getTasks();