-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
43 lines (36 loc) · 1 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
/* Exercise 1: Wish list */
$(document).ready(function() {
$("#item").focus();
var btn = $("#add-to-list");
btn.click(function() {
var value = $("#item").val();
if (value !== "") {
addWishToList(value);
updateTotal();
} else {
console.log("You need to enter a wish in the input to add an item");
}
});
$(document).on('click', '.label.pending', function() {
$(this).switchClass("pending", "success");
$(this).text("Done!");
$(this).parent().switchClass("pending", "completed");
updateTotal();
});
$(document).keypress(function(e) {
if(e.which === 13) {
btn.trigger("click");
}
});
});
function addWishToList(value) {
var list = $("ol#items");
var wish = '<li class = "pending">' + value +
'<span class="label pending">Pending</span></li>';
list.append(wish);
}
function updateTotal() {
var pendingNumber = $(".label.pending").length;
var sucessNumber = $(".label.success").length;
$(".total").text("Total: Pending = " + pendingNumber + " Done = " + sucessNumber);
}