Page 1 of 1

onchange with a if

Posted: Wed Apr 27, 2005 2:02 pm
by shiznatix
i need to change the value of a field whenever a certain option from a dropdown menu is selected. iv tried this:

Code: Select all

<form action=&quote;test.php&quote; method=&quote;post&quote; name=&quote;thisform&quote;>
Price:$16.00<br>
XtraXtra Large: +$2.00<br>
	<select name=&quote;itemName&quote; onchange=&quote;if (this.value=='XXL Black Rx7Club T-shirt Design #2') document.thisform.itemPrice.value='18.00'; else document.thisform.itemPrice.value='16.00';&quote;>
		<option value=&quote;itemName&quote; selected>Choose Shirt Size :</option>
		<option value=&quote;S Black Rx7Club T-shirt Design #2&quote; name=&quote;itemName&quote;>Small</option>
		<option value=&quote;M Black Rx7Club T-shirt Design #2&quote; name=&quote;itemName&quote;>Medium</option>
		<option value=&quote;L Black Rx7Club T-shirt Design #2&quote; name=&quote;itemName&quote;>Large</option>
		<option value=&quote;XL Black Rx7Club T-shirt Design #2&quote; name=&quote;itemName&quote;>Xtra Large</option>
		<option value=&quote;XXL Black Rx7Club T-shirt Design #2&quote; name=&quote;itemName&quote;>XtraXtra Large</option>
	</select>
<input type=&quote;hidden&quote; name=&quote;itemPrice&quote;>
<input type=&quote;submit&quote; name=&quote;submit&quote; value=&quote;Add To Cart&quote;>
</form>
but that fails to do anything. i dont know anything about javascript so any help would be great. thanks.

Posted: Wed Apr 27, 2005 2:49 pm
by Chris Corbyn
You need to write function elsewhere on the page in <script> tags and then call the function, rather than the entire statement.

If you need any help just ask ;-)

Re: onchange with a if

Posted: Thu Apr 28, 2005 1:30 am
by n00b Saibot
change line 4 to this :arrow:

Code: Select all

<select name=&quote;itemName&quote; onchange=&quote;if (this.selectedIndex == 5) this.form.itemPrice.value='18.00'; else this.form.itemPrice.value='16.00';&quote;>
Simple enough ;)