Forum Moderators: open
I have a drop down list that is created from a mySQL database. When a user selects a different item in the list, I want to update a textfield with a subtotal price based on what they have selected.
So I am trying to build a Javascript function "subtotal()" that calls the price of an item selected in the dropdown menu using <select onChange="subtotal()">, adds it on to the figure already there, and displays the current subtotal in a dynamic text box.
Could anyone help me define the Javascript function? Or point me to a working example?
Thank you very much,
Moritz Gaede
[edited by: jatar_k at 9:09 pm (utc) on May 14, 2004]
[edit reason] no emails in sigs thanks [/edit]
[pre]<script>
function subtotal() {
var my_price = eval(document.Form1.ItemPrice.value);
var my_qty = eval(document.Form1.quantity.value);
var my_total = my_price * my_qty;
document.Form1.total.value = my_total;
}
</script>
<form name=Form1>
<select name="ItemPrice" onChange="return subtotal();">
<option value="1">Item 1</option>
<option value="2.95">Item 2</option>
</select>
<input type=text size=3 name=quantity value="2">
total: <input type=text name=total size="5">
</form>
[/pre]