-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyscript.js
98 lines (78 loc) · 2.63 KB
/
myscript.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
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
//Retrieve To-Do Values stored on the local storage
function get_todos()
{
//Initializing the Array of Items
var todos = new Array;
//Seting the array storing address [Local Storage]
var todos_str = localStorage.getItem('todo');
//If the returned value is not empty, then display the stored string value
if (todos_str !== null)
{
//Convert the JSON string back into JavaScript Data
todos = JSON.parse(todos_str);
}
//Return any values stored on the Database
return todos;
}
//Is triggered when the ADD button is Clicked
function add()
{
var task = document.getElementById('task').value;
var todos = get_todos();
// PuSH Appends[ADD] to the existing list [Local Storage]
todos.push(task);
//(STRINGIFY) method converts a JavaScript object or value to a JSON string
localStorage.setItem('todo', JSON.stringify(todos));
show();
//avoid any further actions caused by the CLICK Action
return false;
}
// Clears any input from the INPUT Box after the process is over
function clearDefault(a)
{
//Check for any string value in the INPUT box
if (a.defaultValue==a.value)
{
//set the value into an EMPTY String
a.value=""
}
}
// It removes the selected item to be deleted
function remove()
{
//[This] refers to the current DOM value by index
var id = this.getAttribute('id');
var todos = get_todos();
//Remove a specific from the JavaScript Array
todos.splice(id, 1);
//Store the new task back into the Database
localStorage.setItem('todo', JSON.stringify(todos));
show();
return false;
}
// Display the Current TO-DO list
function show()
{
// Call the Array of Items [TO-DO List]
var todos = get_todos();
// Creating Manual Snippets
var html = '<ul>';
for(var i=0; i<todos.length; i++)
{
//Automatically Add a [REMOVE] Button
html += '<li>' + todos[i] + '<button class="remove" id="' + i + '">Delete</button> </li>';
};
html += '</ul>';
//Insert the newly added Snippets in the original document loaded from the server
//And Replaces the content of the element with the ID [todos]
//And Display the list regardless to what was their earlier
document.getElementById('todos').innerHTML = html;
//
var buttons = document.getElementsByClassName('remove');
for (var i=0; i < buttons.length; i++)
{
buttons[i].addEventListener('click', remove);
};
}
document.getElementById('add').addEventListener('click', add);
show();