-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
62 lines (59 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
let mylocalStorage = JSON.parse(localStorage.getItem("data")) || [];
const checkLocalStorage = () => {
if (mylocalStorage) {
mylocalStorage.forEach((x) => {
$("ol").append(`<li><input type='checkbox' id='${x.id}'>${x.value}</li>`);
});
}
};
checkLocalStorage();
const updateLocalStorage = () => {
localStorage.setItem("data", JSON.stringify(mylocalStorage));
};
const addItem = () => {
const inputBox = $("input:text").val();
const timeId = new Date().getTime();
if (!inputBox.trim()) {
alert("There is nothing to add!");
} else {
$("ol").append(
`<li><input type='checkbox' id='${timeId}'>${inputBox}</li>`
);
$("input:text").val("");
// Local Storage
mylocalStorage.push({
id: `${timeId}`,
value: inputBox,
});
updateLocalStorage();
}
};
$(document).ready(() => {
// Add item on click
$("#addItem").click(addItem);
// Add item after pressing Enter key
$("input:text").keyup((event) => {
if (event.keyCode === 13) {
addItem();
}
});
// Remove item after the checkbox is clicked
$(document).on("click", "input:checkbox", function () {
$(this).parent().addClass("strike").fadeOut("slow");
let selectedItem = event.target.id;
mylocalStorage = mylocalStorage.filter((x) => {
return x.id !== selectedItem;
});
updateLocalStorage();
// alert when all tasks are completed
let x = $("ol").children(":visible").length;
if (x === 1) {
// delay for alert
setTimeout(() => {
alert("You have done everything!");
}, 1000);
}
});
// Drag & Drop (jQuery UI)
$("ol").sortable();
});