javascript: selected value of dropdown

A

arno

Hello,

I have a form with a dropdown list. Now, I would like to know in javascript
what value was selected but I cannot figure that out.

Here are my two scripts that do not work, the first is a "copy" of a script
to read checkboxes, the second one gives me at least the index of the
selection (but not the value). With the alert command I am only displaying
the result:
(Form = myform
fieldname = country)

1)
for(j=0;j<document.myform.country.length;j++) {
if(myform.country[j].checked) {
var myvar=myform.country[j].value
}
}
alert(myvar); /*result ist an empty string*/

2)
k = document.myform.country.selectedIndex;
var myvar2=myform.country[k].value;
alert(k); /*result is the index of the selected item*/
alert(myvar2); /*result is an empty string*/


my dropdown looks like this:
<select name="country" size="1">
<option selected></option>
<option>BELGIUM</option>
<option>BULGARIA</option>
</select>

What is the value of the item selected?

regards


arno
 
J

Jon Spivey

Hi,

it would be
var s = document.formName.DropDown.options
alert(s[s.selectedIndex].value)

you need to find the selected index first and then find the value of the
option with that index.
 
A

arno

Thank you Jon,
it would be
var s = document.formName.DropDown.options
alert(s[s.selectedIndex].value)

you need to find the selected index first and then find the value of the
option with that index.
<option>BELGIUM</option>
I also changed this to
<option value="BELGIUM">BELGIUM</option>

regards

arno
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top