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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

get the option selected from an input 1

Status
Not open for further replies.

tyris

Programmer
Nov 2, 2000
311
FR
hi all,
i want to get the option selected from an input.
i mean i have this in the HTML code :

<select name='type_lang'>
<option value='0'>All</option>
<option value='1'>None</option>
<option value='2'>Arabic</option>
<option value='3'>Brazilian</option>
</select>

When i click a button, i want to call a script that tells me which option has been selected. How can i do ? Am i forced to test each time :
if (myform.type_lang[0].checked==true) ...

???

or is there a lighter way to perform this ??

thanks in advance
best regards,

elise
Best regards,
Elise
 
document.formname.type_lang.options[document.formname.type_lang.selectedIndex].text

will give you the value and text of the option that is selected

jared@aauser.com
 
in IE, btw, you can just use:

document.formname.type_lang.value

and for slighty more optimized version of my initial post:

var opbox = document.formname.type_lang
opbox.options[opbox.selectedIndex].value

should give you a (probably insignificant, unless in a loop) performance boost

jared@aauser.com
 
really really really thank you !
i just needed the &quot;.text&quot; tips because i only knew the &quot;.value&quot; one and that was too long to test more than 10 options ...
all my gratitude

have a nice day ...
elise

Best regards,
Elise
 
for a radio box, do you replace the &quot;.options&quot; by a &quot;.radio&quot; ? or is it totaly different ? Best regards,
Elise
 
That's okay, i found the solution by my own.

To get the value of the currently selected radio button in a radio group :

function checkIt() {
theGroup = document.theForm.gender;
for (i=0; i<= theGroup.length; i++) {
if (theGroup.checked) {
alert(&quot;The value is &quot; + theGroup.value);
break;
}
}
}
Best regards,
Elise
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top