Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to get to value of a selected option in a drop-down box

Status
Not open for further replies.

barny2006

MIS
Mar 30, 2006
521
US
hi,
i have my code working. however, i'm stuck in getting the value of a folder that is in a drop down box. i can't seem to get the correct syntax for getting the value of the selectin.
Code:
<form name=frm2>
     <SELECT NAME="folders" SIZE="1">
        <OPTION SELECTED>app_dev
        <OPTION>research
        <OPTION>finance
        <OPTION>immunization
        <OPTION>HCCA
     </SELECT>
</form>
i can get a number to indicate the selected index of the option that they select. but how would i get the actual value?
Code:
 sel = document.frm2.folders.options.selectedindex
thanks!



 
sel = window.document.getElementsByName("folders").value

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
i'm getting an error message:
"object doesn't support this property or method."
 
What about?
sel = window.document.frm2.getElementsByName("folders").value

and actually you seem to be missing "</OPTION>" after each <OPTION> in the example you posted.
Code:
<form name=frm2>
     <SELECT NAME="folders" SIZE="1">
        <OPTION SELECTED>app_dev</OPTION>
        <OPTION>research</OPTION>
        <OPTION>finance</OPTION>
        <OPTION>immunization</OPTION>
        <OPTION>HCCA</OPTION>
     </SELECT>
</form>

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
so how about?

sel = frm2.folders.value

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
thanks dm
i got it to work after playing with it.
here's the syntax:
Code:
idx = document.frm2.folders.options.selectedindex
txt = document.frm2.folders.options(idx).text
so, i think the syntax is text instead of value.
thanks so much.
 
<option> tags do not require a closing tag.

.text is what the user sees
.value is a property set within the tag <option value="1">

You will receive an error by attempting to get the value if the value is not set.
 
Thanks for that explanation Skie.

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Simply this.
[tt] txt = document.frm2.folders.value[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top