-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
122 lines (87 loc) · 3.27 KB
/
index.js
File metadata and controls
122 lines (87 loc) · 3.27 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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
$(function(){
//Initialize a click function for the 'Add' button
$("#add").on("click",function(){
//Get the value of the input field
var val = $("input").val();
if (val !=='') {
//Showing the clear button
$("#clear").show();
//Create a new list item
var elem = $("<li></li>").text(val);
//Append a delete button to the list item
$(elem).append("<button class='rem btn btn-sm btn-danger' style='float: right;'>×</button>");
//Append a click button to the list item
$(elem).append("<button class='mark btn btn-sm btn-success' style='float: right;'>✓</button>");
//Append the new list item to the list
$("#mylist").append(elem);
//Clear the input field
$("input").val("");
//Calling the delete and mark functions
DeleteMark();
}
//Get the stored value of the task array
console.log("Created a new task");
//Call the function to store the new task
saveTasks();
});
//Saving the tasks to local storage
function saveTasks(){
localStorage.setItem("TaskCont", JSON.stringify($('#mylist').html()));
}
//Creating a funtion for retrieving the tasks from local storage
function getTasks(){
var storedTasks = JSON.parse(localStorage.getItem("TaskCont"));
//If the stored value is not null
if (storedTasks !== null) {
//Append the stored value to the list
$('#mylist').html(storedTasks);
//show the clear button
$("#clear").show();
//calling the delete and mark functions
DeleteMark();
}
}
//The delete and mark click function
function DeleteMark(){
//Add a delete function to the new list item
$(".rem").on("click",function(){
//Remove the list item
$(this).parent().remove();
saveTasks();
});
//create a variable to store the click function
var isMarked = false;
//Add a click function to the new list item
$(".mark").on("click",function(){
//if the list item is not marked
if (isMarked == false) {
//mark the list item
$(this).parent().css({"text-decoration":"line-through", "color": "grey"});
//change the variable to true
isMarked = true;
//else if the list item is marked
}else{
//unmark the list item
$(this).parent().css({"text-decoration":"none", "color": ""});
//change the variable to false
isMarked = false;
}
//Save the tasks
saveTasks();
});
}
//Clear the list and local storage
$("#clear").on("click",function(){
//Clear the list
$("#mylist").empty();
$("#clear").hide();
//clear the local storage
localStorage.clear("TaskCont");
console.log("Cleared the list");
});
//When local storage is empty, hide the clear button
if(localStorage.getItem("TaskCont") === null){
$("#clear").hide();
}
getTasks();
});