Update Price of Selected Item

[Total: 0    Average: 0/5]

This shows how to update a text component of a form on a selection change in the same form.

Head Code:

<script>
var _next=0
var Apple=(_next++);
var Peach=(_next++);
var Pears=(_next++);
var Red_Rose=(_next++);
var prices = new Array();
    prices[Apple]="1.50";
    prices[Peach]="3.25";
    prices[Pears]="0.50";
    prices[Red_Rose]="1.20";
</script>

Body Code:

<form name=exf1>
<select name=s1 onchange='updatePrice()'>
    <option value='Apple'>Apple</option>
    <option value='Peach'>Peach</option>
    <option value='Pears'>Pears</option>
    <option value='Red_Rose'>Red Rose</option>
</select>
<input type=text name=t1 value='?'>
</form>
<script>
// set up the data to be consistent to start with.
document.exf1.s1.selectedIndex=0;
document.exf1.t1.value=prices[0];

function updatePrice() {
var _v = document.exf1.s1.selectedIndex;
var _x = document.exf1.s1[_v].value;
var _y = eval(_x);
document.exf1.t1.value=prices[_y];
}
</script>

Times Viewed: 0

Leave a Reply

Your email address will not be published. Required fields are marked *