-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPRACTICE_index.js
67 lines (48 loc) · 2.22 KB
/
PRACTICE_index.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
66
67
// --------------------------------------------------------------------ADD ITEM-------------------------------------
//NOTE: The code to add an item is overly complicated, it could have been as simple as the delete and check functions. I was experimenting with the "single function" principal of modularity.
//However, I do like how all functions are being called from one place at the bottom called handleShoppingList().
function generateItemHTML(item){
return `<li>
<span class="shopping-item">${item}</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>`
}
function grabInput(event){
return $(event.target).find('input').val();
}
function handleAddItem(){
$('#js-shopping-list-form').on('submit', e=> { //event listener form entering new item
e.preventDefault(); //prevent form submission to non-existent server
let userInput = grabInput(e); //grab and assign user's input to variable
$('.shopping-list').append(generateItemHTML(userInput)); //generate <li> item and append to list
$(e.target).find('input').val(""); //reset input value
})
}
// --------------------------------------------------------------------ADD ITEM-------------------------------------
function handleDelete(){
$('.shopping-list').on('click', '.shopping-item-delete', e => {
console.log($(e.target).closest('li').find('.shopping-item').text())
deleteItem = $(e.target).closest('li');
deleteItem.remove();
})
}
function handleItemCheck(){
$('.shopping-list').on('click', '.shopping-item-toggle', e=> {
console.log($(e.target).closest('li').find('.shopping-item').text())
$(e.target).closest('li').find('.shopping-item').toggleClass('shopping-item__checked')
})
}
function handleShoppingList(){
console.log('DOM loaded and ready...');
handleAddItem();
handleDelete();
handleItemCheck();
}
$(handleShoppingList);