...

View Full Version : Adding values from a drop down list



arc2002
11-11-2004, 03:59 PM
Hi,
I was wondering if anyone could help me? I am making an order form and am trying to calculate the cost. I have two drop down boxes which each have two options. The first is "8:30 - 4:30" & "8:00 - 6:00" now the cost for each are 17.50 and 20.00. I want to be able to automatically calculate the cost when the user selects each day and time.

example
Day 1 [8:00 - 6:00] Day 2 [8:00 - 6:00]
total (40.00)

here is the code for the form i have at present:

<form name="form1" method="post" action="">
Day 1
<select name="day1" id="day1">
<option>8:30 - 4:30</option>
<option>8:00 - 6:00</option>
</select>
Day 2
<select name="day2" id="day2">
<option>8:30 - 4:30</option>
<option>8:00 - 6:00</option>
</select>
<br>
<br>
Total:
<input name="total" type="text" id="total" size="6">
</form>

Thank you for your help, I am just starting out with javascript and can't find anything in books or on the web.

Thanks Again,
Andy

hemebond
11-11-2004, 08:14 PM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<title>47355</title>
</head>
<body>
<form name="form1" method="post" action="">
<fieldset>
<label for="day1">Day 1</label>
<select id="day1" name="day1" onchange="update_total()">
<option value="17.50">8:30 - 4:30</option>
<option value="20.00">8:00 - 6:00</option>
</select>
<label for="day2">Day 2</label>
<select id="day2" name="day2" onchange="update_total()">
<option value="17.50">8:30 - 4:30</option>
<option value="20.00">8:00 - 6:00</option>
</select>
<label for="total">Total</label>
<input id="total" name="total" type="text" size="6">
</fieldset>
</form>
</body>
<script type="text/javascript">
function update_total()
{
document.getElementById("total").value = parseFloat(document.getElementById("day1").value) + parseFloat(document.getElementById("day2").value);
}

update_total();
</script>
</html>Gecko Dom Reference (http://www.mozilla.org/docs/dom/domref/)

arc2002
11-11-2004, 08:33 PM
Thank You so much hemebond , that is exactly what I wanted. Brilliant !
Cheers.

Really sorry to be a pain, but is there any way of being able to have 35.00 instead of 35. Thank you for all your help you people are brilliant!

hemebond
11-11-2004, 10:32 PM
document.getElementById("total").value = Number(Number(document.getElementById("day1").value).toFixed(2) + Number(document.getElementById("day2").value)).toFixed(2);



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum