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
47 lines (33 loc) · 1.08 KB
/
Copy pathjquery-javascript.js
File metadata and controls
47 lines (33 loc) · 1.08 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
// 1. Add jQuery's document.ready function
/* 2. Use jQuery to reference the elements by class,
add a click event and passing the corresponding function on the click */
// Start quantity at 0
var quantity = 0;
// JavaScript functions, no need to changes these
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;
}