-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
75 lines (65 loc) · 2.51 KB
/
index.js
File metadata and controls
75 lines (65 loc) · 2.51 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
66
67
68
69
70
71
/**
* Adds this jQuery interactivity to index.html:
* 1. Enter items to purchase by entering text and hitting "Enter" or clicking the "Add item" button.
* 2. Check and uncheck items on the list by clicking the "check" button
* 3. permanently remove items from the list by clicking the "delete" button
*/
function handleShoppingListEvents() {
// 1. Enter items to purchase by entering text and hitting "Enter" or clicking the "Add item" button.
handleNewItem();
// 2. Check and uncheck items on the list by clicking the "check" button
handleCheckItem();
// 3. permanently remove items from the list by clicking the "delete" button
handleDeleteItem();
}
// 1. Enter items to purchase by entering text and hitting "Enter" or clicking the "Add item" button.
function handleNewItem() {
// listen for a form submission via button click
$('#js-shopping-list-form').on('submit', function(e) {
e.preventDefault();
// store submission value in newItem
const newItem = $(this).find('#shopping-list-entry').val();
if (newItem == "") {
alert(`Can't be empty.`);
} else {
// append the shopping list <ul> with an <li> for the newItem
$('.shopping-list').append(
`<li>
<span class="shopping-item">${newItem}</span>
<div class="shopping-item-controls">
<button class="shopping-item-toggle">
<span class="button-label">check</span>
</button>
<button class="shopping-item-delete">
<span class="button-label">delete</span>
</button>
</div>
</li>`
);
// clear the entry input to prepare for next input submission
$('#shopping-list-entry').val('');
}
});
}
// 2. Check and uncheck items on the list by clicking the "check" button
function handleCheckItem() {
// listen on <ul> for toggle button clicks
$('.shopping-list').on('click', '.shopping-item-toggle', function(e) {
// traverse to the span.shopping-item element
const item = $(this).closest('li').find('.shopping-item');
// manipulate span.shopping-item by toggling the .shopping-item__checked class
item.toggleClass('shopping-item__checked');
});
}
// 3. permanently remove items from the list by clicking the "delete" button
function handleDeleteItem() {
// listen on <ul> for delete button clicks
$('.shopping-list').on('click', '.shopping-item-delete', function(e) {
// traverse to the closest ancestor <li>
const item = $(this).closest('li');
// manipulate the <li> by removing it
item.remove();
});
}
// call event listener handler function on document load
$(handleShoppingListEvents)