[thelist] Simple JavaScript question

Rick den Haan rick.denhaan at gmail.com
Sat Jul 8 10:51:57 CDT 2006


John,

john at johnallsopp.co.uk wrote:
> <script language='JavaScript'>
> 	function changePrice(pOption)
> 	{
> 	 // get what user selected
> 	 ind = document.giftOpts.pOption.selectedIndex;
> 	 val = document.giftOpts.pOption.options[ind].text;
> 	 window.alert ('Debug: Inside javascript: changePrice');
> 	}
> </script>";
>   
Here's how I'd change this to get the value of the selected option:

ind = document.getElementById(pOption).selectedIndex;
val = document.getElementById(pOption).options[ind].value;
> The select part goes like this:
>
> <form method='post' action='index.php4' name='giftOpts'><select
> name='abc' onChange='changePrice(abc)'>";
>   
You're missing quotes around your abc in onChange='changePrice(abc)'. 
Without quotes, Javascript will treat that as a variable name. If you 
haven't defined a variable abc, this will end up sending "undefined" to 
the function. Putting quotes around that will solve your problem;

<select name='abc' onChnage=\"changePrice('abc')\">

Note the backquotes, they're there because I see it's probably coming 
from a PHP echo statement (given the "; at the end there).

Good luck,

Rick.



More information about the thelist mailing list