[RESOLVED] javascript listbox not working
I've made a taxi fare calculator using google maps, and now im trying to add extra functionality, but i cant get the following code to work.
The idea is that once its calculated and displayed the fare in "txtFare", then to find the fare for a "bus", its selected from the list box and multiply's "txtFare" by '1.50' and displays it in "txtTypeFare".
This function doesn't work, and i can't see why not.
Code:
function calcType() {
var VehType = document.getElementById("sVehicle").innerHTML;
var EstFare = document.getElementById("txtFare").value;
if (VehType = "car")
{document.getElementById("txtTypeFare").value = EstFare};
if (VehType = "estate")
{document.getElementById("txtTypeFare").value = EstFare * 1.25 };
if (VehType = "bus")
{document.getElementById("txtTypeFare").value = EstFare * 1.50};
if (VehType = "exec")
{document.getElementById("txtTypeFare").value = EstFare * 1.75};
}
HTML Code:
<b>Fare: </b>
<input id="txtFare" type="textbox" value="">
<input type="button" value="fare" onClick="calcFare();">
<b>Vehicle: </b>
<select id="sVehicle" onChange="calcType()">
<option value="car">Car</option>
<option value="estate">Estate</option>
<option value="bus">Mini-Bus</option>
<option value="exec">Executive</option>
</select>
<input id="txtTypeFare" type="textbox" value="">
Thanks in advance
Re: javascript listbox not working
This line is a problem:
Code:
var VehType = document.getElementById("sVehicle").innerHTML;
If you want to see what the VehType variable is being populated with, add alert(VehType); on the line after that; it's not what you want.
Switch to:
Code:
var VehType = document.getElementById("sVehicle").value;
Also, on your if statements, you're using = instead of ==. That means, in all of your if statements, VehType is being assigned a value, not evaluated for a value, and all of your if statements are evaluating to true (because variable assignment evaluates to true). So the only if statement that you'll see the effect of is the last one.
Re: javascript listbox not working
thanks for the advice on ==, its working correctly now.
chris1990
Re: javascript listbox not working
If your question is answered then mark your thread RESOLVED and give credit to whoever answered it. ;)