forked from pearlchen/LLC-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathjquery-javascript.js
More file actions
60 lines (45 loc) · 1.34 KB
/
Copy pathjquery-javascript.js
File metadata and controls
60 lines (45 loc) · 1.34 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
$(document).ready(function(){
/* Using jQuery to reference the elements by class,
adding a click event and passing the function on click */
// Referencing the button with the 'add' class
$(".add").click(function(){
//call the appropriate function
addItem();
});
$(".removeItem").click(function(){
removeItem();
});
$(".checkout").click(function(){
checkout();
});
// Start quantity at 0
var quantity = 0;
// JavaScript functions
function addItem(){
quantity = quantity + 1;
refreshTotal();
}
function removeItem(){
if ( quantity > 0 ) {
quantity = quantity - 1;
} else {
// do nothing
}
refreshTotal();
}
function checkout() {
if ( quantity <= 0 ) {
alert( "Sorry, you don't have anything in your cart!" );
} else {
alert( "Thank you for your order!" );
}
}
function refreshTotal() {
// calculate the totalCost with a fixed price of $20.
var totalCost = quantity * 20;
// then update the quantity text field
document.getElementById('updateQuantity').value = quantity;
// update the total cost text field
document.getElementById('updateTotal').value = "$" + totalCost;
}
});//closes the document.ready