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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
it won't take long... 30 is nothing.
BUT, if you want to do it another way:
<body onload="document.Form1.dro pdown.valu e = 'UK';">
though i'm not sure that it is supported by all browsers
BUT, if you want to do it another way:
<body onload="document.Form1.dro
though i'm not sure that it is supported by all browsers
<body onload="document.forms[0]. dropdown.s electedInd ex = 1">
<form name="form1" method="post" action="">
<select name="dropdown">
<option value="CH">China</option>
<option value="UK">United Kingdom</option>
</select>
</form>
</body>
<form name="form1" method="post" action="">
<select name="dropdown">
<option value="CH">China</option>
<option value="UK">United Kingdom</option>
</select>
</form>
</body>
or just use the SELECTED keyword in the option:
<option value="UK" SELECTED>United Kingdom</option>
<option value="UK" SELECTED>United Kingdom</option>
true
ASKER
Thanks guys for the help
ASKER
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