Reputation: 3
I have a problem with a script and I don't know how to handle it. I'm trying to make a simple calculator that displays how much you must pay for a random model. I'm trying to display a different answer depending upon the amount of time and advance chosen in the select menu. The formulas in each if statement does actually work when I simply list the variables outside of if statements displaying each answer in a series of alert boxes, but I'd like to display only the applicable answer.What I am doing wrong? Escuse my rudimentary JS skills butt I am still a beginner in this domain.Thanks for your time.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Calculator</title>
<script script language="javascript" type="text/javascript">
function rata() {
var selection1 = document.getElementsById("model")[0].value;;
var selection2 = document.getElementsById("time")[1].value;;
var selection3 = document.getElementsById("advance")[2].value;;
if(selection2 === "t1" && selection3 === "a1"){
var pay1 = "100 euro";
alert("You must pay" + pay1 + ".");
}
else if(selection2 === "t1" && selection3 === "a2"){
var pay2 = "200 euro";
alert("You must pay" + pay2 + ".");
}
else if(selection2 === "t2" && selection3 === "a1"){
var pay3 = "400 euro";
alert("You must pay" + rata3 + ".");
}
else if(selection2 === "t2" && selection3 === "a2"){
var pay4 = "800 euro";
alert("You must pay" + pay4 + ".");
}
}
</script>
</head>
<body>
<form name="calculator">
Model <br><select name="model" id="model">
<option value="">selectati modelul</option>
<option value="model">Model 1</option>
<option value="model">Model 2</option>
<option value="model">Model 3</option>
<option value="model">Model 4</option>
<option value="model">Model 5</option>
<option value="model">Model 6</option>
<option value="model">Model 7</option>
<option value="model">Model 8</option>
</select><br>
Time <br><select name="time" id="time">
<option value="">select time</option>
<option value="t1">1 year</option>
<option value="t2">2 years</option>
</select> <br>
Advance <br><select name="advance" id="advance">
<option value="" >select advance</option>
<option value="a1" >0%</option>
<option value="a2" >50%</option>
</select> <br>
<input type="button" value="Send" onclick="pay()">
</form>
</body>
</html>
Upvotes: 0
Views: 327
Reputation: 114347
<input type="button" value="Send" onclick="pay()">
<--- this calls "pay()", but your function is called "rata()"
document.getElementsById("model")[0].value;
<-- there is no such function, it's not plural, it should be: getElementById
document.getElementById("model")[0].value;
will never give you the value of a SELECT. Use this:
var sel1 = document.getElementById("model")
var val1 = sel1[sel1.selectedIndex].value
Since you're getting the value, and all of the values are "model" you will get the same value regardless of what the user chooses.
Upvotes: 1