I’m trying to create a javascript code to calculate the total cost of services which the user selects. The total cost should be updated live in the cart as the user selects any service. Discount and taxes are also meant to be taken into account. The code that I’ve tried will be provided below.
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="IE=Edge">
<<link rel="stylesheet" href="style.css">
<title>TribalGateway - Airport Escort</title>
<h1>Airport Escort</h1>
<p>
Thank you for the opportunity to allow us to make your visit to Nigeria as
stress-free as possible. Our Airport Escort will greet you once you arrive
and guide you through the Immigration Lines as quickly as possible. You
can have one loved one meet you right outside of the Security Door. To
customize your experience, please select your preferences below.
</p>
<form action="#" method="post">
<fieldset>
<legend>Personal Information</legend>
<label for="name">Name:</label>
<input type="text" id="name" name="name" required /><br />
<label for="phone">Phone:</label>
<input type="tel" id="phone" name="phone" required /><br />
<label for="email">Email Address:</label>
<input type="email" id="email" name="email" required /><br />
<label for="arrival-date">Date of Arrival:</label>
<input type="date" id="arrival-date" name="arrival-date" required /><br />
<label for="arrival-time">Time of Arrival:</label>
<input type="time" id="arrival-time" name="arrival-time" required /><br />
<label for="airline">Airline:</label>
<input type="text" id="airline" name="airline" required /><br />
<label for="departure-date">Date of Departure:</label>
<input type="date" id="departure-date" name="departure-date" required /><br />
<label for="persons-arriving">How many persons will be arriving?</label>
<input type="number" id="persons-arriving" name="persons-arriving" required /><br />
</fieldset>
<fieldset>
<legend>Escort Service</legend>
<p>I NEED ESCORT SERVICE FOR:</p>
<label>
<input type="checkbox" name="escort-service" value="Arrival Only" /> Arrival Only
$150pp
</label><br />
<label>
<input type="checkbox" name="escort-service" value="Departure Only" /> Departure Only
$150pp
</label><br />
<label>
<input type="checkbox" name="escort-service" value="Both Arrival and Departure" /> Both
Arrival and Departure $275
</label><br />
<p>Will someone be in Nigeria to greet you at the airport?</p>
<label for="greet-name">Name:</label>
<input type="text" id="greet-name" name="greet-name" /><br />
<label for="greet-phone">Phone#:</label>
<input type="tel" id="greet-phone" name="greet-phone" /><br />
<label for="emergency-contact">Emergency Contact in Home Country:</label>
<input type="text" id="emergency-contact" name="emergency-contact" /><br />
</fieldset>
<fieldset>
<legend>TRANSPORTATION</legend>
Please indicate if you will need transportation on the day of your Arrival and Departure.
I will need transportation from the airport to my housing accommodations on the day of my Arrival. ($50)
I will need transportation from my housing accommodation to the airport on the day of my Departure. ($50)
For my arrival, I will need transportation for my Relative to be picked up and brought to the airport to greet me, and then take us both to our accommodation from the airport ($100)
I will also need this for my Departure. The driver will pick us up from our accommodation and take us to the airport. The driver will wait at the airport while my Relative sees me off. Then the driver will take my Relative back to the accommodations. ($100)
<legend>SECURITY</legend>
<p>Would you like to have a security officer to escort you for the day?</p>
<input type="radio" name="security" value="no" id="security-1" checked>
<label for="security-1">No</label>
<br>
<input type="radio" name="security" value="private-officer" id="security-2">
<label for="security-2">Private Officer ($50 daily)</label>
<br>
<input type="radio" name="security" value="police-officer" id="security-3">
<label for="security-3">Police Officer ($70 daily)</label>
<br>
<input type="checkbox" name="security" value="visibly-armed" id="security-4">
<label for="security-4">Visibly Armed ($50 daily surcharge, in addition to above Officer Charge)</label>
<legend>T-SHIRTS</legend>
<p>Queen Tshirt $25</p>
<input type="checkbox" name="tshirt" value="queen-s" id="tshirt-1">
<label for="tshirt-1">S</label>
<input type="checkbox" name="tshirt" value="queen-m" id="tshirt-2">
<label for="tshirt-2">M</label>
<input type="checkbox" name="tshirt" value="queen-l" id="tshirt-3">
<label for="tshirt-3">L</label>
<input type="checkbox" name="tshirt" value="queen-xl" id="tshirt-4">
<label for="tshirt-4">XL</label>
<input type="checkbox" name="tshirt" value="queen-1x" id="tshirt-5">
<label for="tshirt-5">Queen T-Shirt (1X)</label>
0
1
2
3
4
5
Matching King T-Shirt:
King T-Shirt (M)
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
King T-Shirt (L)
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
King T-Shirt (XL)
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
King T-Shirt (1X)
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<legend>CART</legend>
Escort Services $<span id="escort-total">0</span><br>
Transportation $<span id="transportation-total">0</span><br>
Security $<span id="security-total">0</span><br>
T-Shirts $<span id="tshirt-total">0</span><br>
Vat/Taxes $<span id="vat-total">10</span><br>
<hr>
SUBTOTAL $<span id="subtotal">0</span><br>
DISCOUNT CODE: <input type="text" id type="text" name="discount_code" id="discount_code"><br>
Discount Amount $0
YOUR TOTAL IS: $0
APPLY DISCOUNT CODE
SUBMIT
Payments can be sent via
World Remit/Remitly/Sendwave/Western Union
CHOOSE BANK DEPOSIT
Roberta Toniatte Bryant
Lekki, Lagos, Nigeria
[email protected]
+234-913-903-0389
Access Bank
Acct#: 1662807523
// add event listeners to checkboxes
const checkboxes = document.querySelectorAll(‘input[type=”checkbox”]’);
checkboxes.forEach(checkbox => checkbox.addEventListener(‘click’, updateTotal));
// calculate total cost and update cart
function updateTotal() {
let total = 0;
// calculate escort service cost
const escortServices = document.querySelectorAll(‘input[name=”escort-service”]:checked’);
escortServices.forEach(service => {
if (service.value === “Arrival Only” || service.value === “Departure Only”) {
total += 150;
} else if (service.value === “Both Arrival and Departure”) {
total += 275;
}
});
// calculate transportation cost
const transportation = document.querySelectorAll(‘input[name=”transportation”]:checked’);
transportation.forEach(service => {
if (service.value === “airport-to-accommodation” || service.value === “accommodation-to-airport”) {
total += 50;
} else if (service.value === “relative-pickup” || service.value === “relative-dropoff”) {
total += 100;
}
});
// calculate tax and discount
const tax = total * 0.05;
const discountCode = document.querySelector(‘#discount-code’).value;
let discount = 0;
if (discountCode === “TGWLUV23”) {
discount = total * 0.1;
}
// update cart
const cart = document.querySelector(‘#cart’);
cart.innerHTML = `
Cart
Escort Service: $${total.toFixed(2)}
Tax: $${tax.toFixed(2)}
Discount: $${discount.toFixed(2)}
Total: $${(total + tax – discount).toFixed(2)}
`;
}