Link to home
Start Free TrialLog in
Avatar of daz_oldham
daz_oldham

asked on

Set selected index in drop down select

Hi

I have got a dropdown select (of countries).  

As part of my page generation, I want to set the selected index of a dropdown via my javascript.

So, say in my dropdown list (of EVERY country) I wanted to select the United Kingdom, its value would be set to "UK" - here is a quick example.

<select name="dropdown">
<option value="CH">China</option>
<option value="UK">United Kingdom</option>
</select>

So in my Javascript, i want to tell it to select "UK".   But not index number [1] if you understand what I mean, I want to use the actual value.

Many thanks

Daz
ASKER CERTIFIED SOLUTION
Avatar of bobbit31
bobbit31
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of daz_oldham
daz_oldham

ASKER

Hey bobbit, thanks for that :)

is there no way to automatically select that value?

I should imagine if the list of countries is about 30 or so, maybe even more - then that is going to take a long time to execute on the clients side?

Thanks

Daz
it won't take long... 30 is nothing.

BUT, if you want to do it another way:

<body onload="document.Form1.dropdown.value = 'UK';">

though i'm not sure that it is supported by all browsers

<body onload="document.forms[0].dropdown.selectedIndex = 1">
<form name="form1" method="post" action="">
<select name="dropdown">
<option value="CH">China</option>
<option value="UK">United Kingdom</option>
</select>
</form>
</body>
Avatar of knightEknight
or just use the SELECTED keyword in the option:

<option value="UK" SELECTED>United Kingdom</option>
Thanks guys for the help