-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
47 lines (43 loc) · 1.67 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
//input box
const inputBox = document.getElementById("input-box");
//list container
const listContainer = document.getElementById("list-container");
//the on click function for the button
function addTask(){
if(inputBox.value === ''){
alert("You must write something!");
}
else{
let li = document.createElement("li"); //creates a li HTML element
li.innerHTML = inputBox.value; //assigning the text of the li to be the input box value
listContainer.appendChild(li); //puts the li in the list container
//add cross icon
let span = document.createElement("span");
span.innerHTML = "\u00d7";
li.appendChild(span)
}
inputBox.value = ""; //clears input after element is added
saveData(); //whenever we make changes, this is called
}
//if click on list container, check if it is list item or span
//if list item, check and cross
//if span, delete list item
listContainer.addEventListener("click", function(e){
if(e.target.tagName === "LI"){ //if we clicked on li
e.target.classList.toggle("checked"); //toggle checked (cross element and check circle)
saveData();
}
else if(e.target.tagName === "SPAN"){ //if clicked on cross
e.target.parentElement.remove(); //delete li
saveData();
}
}, false)
//NOW WE STORE THE LIST CONTAINER ON BROWSER (if reloaded then we don't want our list to reset)
function saveData(){
localStorage.setItem("data", listContainer.innerHTML); //list container is stored locally
}
//display the data
function showTask(){
listContainer.innerHTML = localStorage.getItem("data");
}
showTask();