Click to See Complete Forum and Search --> : How to know the value of a select.


arturion
08-18-2003, 12:14 PM
I have a code which contents a select:

<form name="the_form">
<select name="the_select">
<option selected>option0
<option>option1
<option>option2
</select>
</form>

then, if I put

window.document.the_form.the_select.options[1].text

it will return the value "option1"

but I want to know how to make the script to return the value that is selected

I don't know if exists something like:

window.document.the_form.the_select.option[selected].text

or something like that

Khalid Ali
08-18-2003, 12:16 PM
this will give you the selected item value

var val = document.the_form.the_select.options[document.the_form.the_select.selectedIndex].value;

Mr J
08-18-2003, 04:55 PM
<script>
function test1(){
var s1=document.f1.show_value.options[document.f1.show_value.selectedIndex].value;
var s2=document.f1.show_value.options[document.f1.show_value.selectedIndex].text;
alert("The option value = "+s1+"\n\nThe option text = "+s2)
}
</script>
Referencing the option value and text.
<form name="f1">
<select name="show_value" onchange="test1()">
<option value="0">Show Value</option>
<option value="1">Value 1</option>
<option value="2">Value 2</option>
<option value="3">Value 3</option>
</select>
</form>