[thelist] Javascript: Passing Text from a Select to a function

Tom Dell'Aringa pixelmech at yahoo.com
Fri Nov 14 13:42:14 CST 2003


--- phillipmvector at comcast.net wrote:
> What I'm trying to do is to pass the selection made on the form to
> a function. When the alert comes up, it says "[Object]". 
> 
> How do I have the selection made on the Listbox pass to the
> Function?
> 
> =====
> function CheckClan(Clan)
> {
> alert(Clan);
> }
> 
> <select name="Clan" onchange="CheckClan(this);">
>    <option selected>Please Select</option>
>    <option>McCloud</option>
> </select>

Philip, it says OBJECT because that is exactly what you are passing
the function - the SELECT object. You need to step into the OPTIONS
array, then send the selectedIndex (the item selected) as the index
item. Further you need to add a VALUE attribute to the OPTION so it
gets sent:

<select name="Clan"
onchange="CheckClan(this.options[selectedIndex].value);">
   <option selected>Please Select</option>
   <option value="McCloud">McCloud</option>
</select>

That should work for you.

HTH

Tom


=====
http://www.pixelmech.com/ :: Web Development Services
http://www.DMXzone.com/ :: JavaScript Author / Every Friday!
http://www.thywordistruth.net/ :: Eternal Life

"I'll ho ho and ha ha you!" (Daffy Duck)


More information about the thelist mailing list