-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
65 lines (57 loc) · 1.77 KB
/
app.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
const temp = $(".todo").clone()[0];
$("#adding button").click(() => {
let value = $("#adding input").val();
addTodo(value);
save();
});
$("#container").on("click", ".del", removeTodo);
$("#container").on("click", ".edit", changeTodo);
function addTodo(value) {
if (value) {
const contain = $("#container");
const todo = $(temp).clone();
$(todo).children("h2").text(value);
contain.append(todo);
$("#adding input").val("");
}
}
function removeTodo() {
$(this).parent().parent().remove();
save();
}
function changeTodo(){
$("button").prop('disabled', true); // Disable all the buttons
$("input").prop('disabled', true);
const contain = $(this).parent().parent();
textPast = contain.children("h2").text();
contain.children("h2").remove();
contain.prepend($("<div>", {"class" : "changeTodo"}));
$(".changeTodo").append($("<input>").val(textPast));
$(".changeTodo").append($("<button>").text("ADD"));
$(".changeTodo input").focus();
$("#container").on("click", ".changeTodo button", function(){
let value = $(".changeTodo input").val();
const parent = $(this).parent().parent();
$(this).parent().remove();
parent.prepend($("<h2>").text(value));
$("button").prop('disabled', false); // Enable all the button
$("input").prop('disabled', false);
});
save();
}
function save(){
const data = [];
for (let i of $("h2")){
// data[$("h2").index(i)] = $(i).text();
data.push($(i).text());
}
localStorage.setItem("data", JSON.stringify(data));
}
function load(){
const data = JSON.parse(localStorage.getItem("data"));
$(".todo").remove();
for (let h of data){
addTodo(h);
}
}
window.onload = load;