I’m building a car insurance calculator which gives users to ability to select a car model and their age range, and upon clicking a button, a figure should pop up that gives the user the price of insurance according to this.
The if statement I’ve made, which uses the .checked and .selected methods aren’t functioning and I can’t work out why.
When i remove the second half of the if statement (&& document.getElementByID(“band1”).selected == true), it works fine.
What could be the problem? See full block of relevant code below
Function:
function calculateInsurance(){
var msg = document.getElementById('insurance');
var poloinsurance1 = poloPrice * bandone;
var poloinsurance2 = poloPrice * bandtwo;
var poloinsurance3 = poloPrice * bandthree;
var poloinsurance4 = poloPrice * bandfour;
if (document.getElementById("VWPoloCheck").checked == true && document.getElementByID("band1").selected == true) {
document.getElementById('insurance').innerHTML = "£" + poloinsurance1;
}
}
HTML:
<table>
<tr>
<th> </th></th>
<td><input type="checkbox" id="VWPoloCheck" value="3500" onclick="poloCheck();"></input></td>
<tr>
</table>
<div>
<select>
<option id="band1">16-24</option>
<option id="band2">25-32</option>
<option id="band3">33-50</option>
<option id="band4">51-75</option>
</select>
</br>
<button id="insurancecost" style="margin-top: 2.5%;" onclick="calculateInsurance();"> Calculate </button>
<p id="insurance"></p>
</div>
Monty is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.