-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjq.js
More file actions
82 lines (59 loc) · 2.03 KB
/
jq.js
File metadata and controls
82 lines (59 loc) · 2.03 KB
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
// This makes sure the document exists before your code tries to manipulate it.
$(document).ready(function () {
// Store references to elements to target
var inputBox = $('#inputBox');
var toDoList = $('#toDoList');
var addNewItemBtn = $('#btn');
// Bind an event handler to the "click" JavaScript event on our button
addNewItemBtn.on("click", function () {
// invoke function to add new item
addNewItem();
});
// add functionality for 'enter' key, Bind an event handler to the "keypress" JavaScript event
inputBox.on( "keypress", function(e) {
// returns which keyboard key or mouse button was pressed for the event
var key = e.which || e.keyCode;
if (key === 13) { // 13 is enter
// invoke function to add new item
addNewItem();
} else {
// do nothing
}
});
toDoList.on("click", function () {
// store reference to item that is clicked
var itemRemove = $(event.target);
// store reference to parent of item that is clicked
var itemParent = itemRemove.parent();
// change the styling of the item that is clicked
itemRemove.css('textDecoration', 'line-through');
// check that the parent is not the entire list
if (itemParent.is('ul')) {
// create a timer that wait 1 sec before removing the list item
setTimeout(function(){
itemRemove.remove();
}, 1000);
} else {
// if item clicked is anchor tag, remove list item
setTimeout(function(){
itemParent.remove();
}, 1000);
}
});
// function to add new item to the list
function addNewItem() {
if (inputBox.val() === '' || inputBox.val() === inputBox.prop("defaultValue")) {
// notify user that value is empty
alert('Please add a new item.');
} else {
// create new element
var newItem = $('<li></li>')
// create element, add element to new element containing input value
newItem.html('<a href="#">' + inputBox.val() +'</a>');
// add created item to entire list
toDoList.append(newItem);
// clear input value from input element
inputBox.val("");
}
}
});