-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.js
69 lines (61 loc) · 2.09 KB
/
code.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
var todoArray = [];
var todoKey = "todos";
$(document).ready(function() {
// check whether we have todos in the localStorage
if (localStorage.getItem(todoKey)) {
//1. Add the todos into the todoArray
var rawJSON = localStorage.getItem(todoKey)
todoArray = JSON.parse(rawJSON);
//2. Populate list with Todos
// Go through each todo in the array
// and add it to our html list, so it looks like
// It's loaded
todoArray.forEach(function(task) {
$("#tasks").append("<li> <input type=\"checkbox\" onclick=\"todoChecked(this)\">" + task + "</input></li>");
});
}
});
function clearCheckedTodos() {
window.navigator.vibrate([200, 100, 200]);
// 1. Use Jquery to loop through all the checked items
// https://api.jquery.com/each/
$(".checked").each(function() {
// when using the jquery each function, 'this' is holding
// the checked item we're currently on. Let's store it on a variable
var item = this;
var todoToRemove;
// get the text content (the title) of the
// list item and trim the white spaces from
// before and after
todoToRemove = item.textContent.trim();
var i = todoArray.indexOf(todoToRemove);
if (i != -1) {
todoArray.splice(i, 1);
}
});
update_storage(todoArray);
// remove all elements with the checked class
$(".checked").remove();
}
function addTodo() {
var todoTitle;
todoTitle = $("#todo-name").val();
if (todoTitle === "") {
return;
}
todoArray.push(todoTitle);
update_storage(todoArray);
$("#tasks").append("<li> <input type=\"checkbox\" onclick=\"todoChecked(this)\">" + todoTitle + "</input></li>");
$("#todo-name").val("");
}
function todoChecked(el) {
if ($(el).is(":checked") === true) {
$(el).parent().addClass("checked");
window.navigator.vibrate(200);
} else {
$(el).parent().removeClass("checked");
}
}
function update_storage(todos) {
localStorage.setItem(todoKey, JSON.stringify(todos));
}