-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
73 lines (52 loc) · 1.73 KB
/
script.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
const container = document.querySelector('.container');
const form = document.querySelector('form')
const title = document.getElementById('title');
const description = document.getElementById('description');
const tasks = localStorage.getItem("tasks")? JSON.parse(localStorage.getItem("tasks")):[];
showAllTasks();
function showAllTasks(){
tasks.forEach((element,index) => {
const div = document.createElement('div')
div.setAttribute('class','task')
const innerdiv = document.createElement('div')
div.appendChild(innerdiv)
const p = document.createElement('p')
p.innerText = element.title
innerdiv.append(p)
console.log(p)
const span = document.createElement('span')
span.innerHTML = element.description
innerdiv.append(span)
const btn = document.createElement('button')
btn.setAttribute('class','deleteBtn')
btn.innerText = "-";
div.append(btn)
container.append(div)
btn.addEventListener('click',()=>{
removeTasks();
tasks.splice(index,1);
localStorage.setItem("tasks",JSON.stringify(tasks))
showAllTasks();
})
});
}
function removeTasks(){
tasks.forEach(()=>{
const div = document.querySelector(".task")
div.remove();
})
}
form.addEventListener("submit",(e)=>{
//the page will be not reloaded by default.
e.preventDefault();
removeTasks();
tasks.push({
title:title.value,
description: description.value
});
localStorage.setItem('tasks',JSON.stringify(tasks))
showAllTasks();
// after showing the input field should be clear
title.value = ""
description.value = ""
})