-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
97 lines (87 loc) · 3.56 KB
/
index.html
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Tip Calculator</title>
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<head>
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<!-- Inline CSS (may need this for repo to work)
<style>
/* Add custom CSS styling here */
</style>
-->
</head>
<body>
<div class="container">
<h1>Tip Calculator</h1>
<form id="tipCalculatorForm">
<div class="form-group">
<label for="billTotal">Bill Total</label>
<input type="number" class="form-control" id="billTotal" placeholder="Enter bill total" required>
</div>
<div class="form-group">
<label for="tipInput">Tip Percentage</label>
<input type="range" class="form-control-range" id="tipInput" min="0" max="100" step="1">
</div>
<div class="form-group">
<label for="tipPercentage">Tip Percentage Value</label>
<input type="text" class="form-control" id="tipPercentage" disabled>
</div>
<div class="form-group">
<label for="tipAmount">Tip Amount</label>
<input type="text" class="form-control" id="tipAmount" disabled>
</div>
<div class="form-group">
<label for="totalBill">Total Bill with Tip</label>
<input type="text" class="form-control" id="totalBill" disabled>
</div>
</form>
</div>
</p>
<!-- Bootstrap JS and jQuery -->
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
<!-- JavaScript -->
<script>
// Get references to form elements
const billTotalInput = document.getElementById('billTotal');
const tipInput = document.getElementById('tipInput');
const tipPercentageInput = document.getElementById('tipPercentage');
const tipAmountInput = document.getElementById('tipAmount');
const totalBillInput = document.getElementById('totalBill');
// This function calculates tip amount and total bill
function calculateTip() {
// Get the values from input fields
const billTotal = parseFloat(billTotalInput.value);
const tipPercentage = parseInt(tipInput.value);
// Calculate tip amount and total bill
const tipAmount = (billTotal * tipPercentage) / 100;
const totalBill = billTotal + tipAmount;
// Update disabled input fields with calculated values
tipPercentageInput.value = tipPercentage + '%';
tipAmountInput.value = tipAmount.toFixed(2);
totalBillInput.value = totalBill.toFixed(2);
}
// EVENT LISTENERS: input and change events
// This event listener restricts user input to two decimal places in billTotal field
billTotalInput.addEventListener('input', function() {
// Get the value entered by the user
let billTotalValue = billTotalInput.value;
// Check if the value has more than two decimal places
const decimalIndex = billTotalValue.indexOf('.');
if (decimalIndex !== -1 && billTotalValue.length - decimalIndex > 3) {
// Shorten value to two decimal places
billTotalValue = parseFloat(billTotalValue).toFixed(2);
// Update input field with shortened value
billTotalInput.value = billTotalValue;
}
});
tipInput.addEventListener('input', calculateTip);
</script>
</body>
</html>