onchange with a if

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

onchange with a if

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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 ;-)
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Re: onchange with a if

Post 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 ;)
Post Reply