I am trying to create a simple calculate function and I don't understand what am I missing. When clicking on the button: "calculate price" nothing is happening. Do you know why?
What I am trying to do is that once the user clicks on calculate price, the calculate() function will run and display a message according to the conditions.
<script>
function calculate(e) {
var personType = document.getElementsById("selectbox");
var ways = document.getElementById("one");
var coupon = document.getElementById("coupon");
var total = 0;
if (personType == "child") {
total = 30;
}
if (personType == "adult") {
total = 40;
}
if (personType == "pensioner") {
total = 25;
}
if (ways == "two") {
total = total * 2;
total = total - 10;
}
if (coupon.includes("19")) {
total = total * 0.9;
document.getElementById("error").innerHTML = total;
}
else {
e.preventDefault();
document.getElementById("error").innerHTML = "coupon is not valid";
}
}
</script>
<form action="activate.php" method="get">
<select name="selectbox" id="selectbox">
<option value="child" id="1">child</option>
<option value="adult" selected id="2">adult</option>
<option value="pensioner" id="3">pensioner</option>
</select>
<fieldset>
<legend>Select Direction:</legend>
<label for="optionradio" >One way</label>
<input type="radio" id="radio" value="one" name="one">
<label for="optionradio">Two way</label>
<input type="radio" id="radio" value="two" name="one">
</fieldset><br />
<label for="coupon">Coupon code:</label>
<input type="text" id="coupon" name="coupon"><br /><br />
<input type="submit" value="buy a ticket">
</form>
<input type="button" value="calculate price" onclick="calculate()"><br />
<p id="error"></p>
Thank you.
if(..)conditions will work as you expect. you need to use two==or three===for equality in javascript.