I´m working on a form that asks the user to enter ticket time preference as well as state of origin to determine tax amount. (California´s tax percentage is different to other states). The tax amount is applied to the ticket time selection, which gives the total amount of the ticket.
The first row, choosing California as state of origin, works fine BUT when I change the state the total amount does not change. I'm not sure where I am going wrong.
<form action="">
<label for="states">Choose your state:</label>
<select
name="states"
id="selectState"
onchange="getTax(this.value)"
></select
><br />
<br />
<label for="ticketTime">Select your ticket:</label>
<br />
<input
type="radio"
name="ticketTime"
onclick="getTicketCost(this.value)"
value="Morning"
/>Morning<br />
<input
type="radio"
name="ticketTime"
onclick="getTicketCost(this.value)"
value="Afternoon"
/>Afternoon<br />
<input
type="radio"
name="ticketTime"
onclick="getTicketCost(this.value)"
value="Night"
/>Night<br />
</form>
<p id="priceOfTicket"></p>
And this is javascript I have written:
//create array to hold values for dropdown
var states = [
'Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California', 'Colorado', 'Connecticut',
'Delaware', 'Florida', 'Georgia', 'Hawaii', 'Idaho', 'Illinois', 'Indiana', 'Iowa', 'Kansas',
'Kentucky', 'Louisiana', 'Maine', 'Maryland', 'Massachusetts', 'Michigan', 'Minnesota',
'Mississippi', 'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire', 'New Jersey',
'New Mexico', 'New York', 'North Carolina', 'North Dakota', 'Ohio', 'Oklahoma', 'Oregon',
'Pennsylvania', 'Rhode Island', 'South Carolina', 'South Dakota', 'Tennessee', 'Texas', 'Utah',
'Vermont', 'Virginia', 'Washington', 'West Virginia', 'Wisconsin', 'Wyoming'
];
var selectState = document.getElementById('selectState');
var contents;
//for loop append option to select tag
for (let i = 0; i < states.length; i++) {
contents += '<option>' + states[i] + '</option>';
}
selectState.innerHTML = contents;
//function to get tax per state
function getTax(state) {
if (state === 'California') {
tax = 0.06;
} else {
tax = 0.08;
}
return tax;
}
//function to calculate the cost of ticket
function getTicketCost(ticketTime) {
var morningTicket = 20;
var afternoonTicket = 25;
var nightTicket = 15;
var ticketCost = 'Total price of ticket ';
if (ticketTime === 'Morning') {
totalCost = morningTicket * tax + morningTicket;
} else if (ticketTime === 'Afternoon') {
totalCost = afternoonTicket * tax + afternoonTicket;
} else {
totalCost = nightTicket * tax + nightTicket;
}
document.getElementById('priceOfTicket').innerHTML =
ticketCost + '$' + totalCost;
}