PDA

View Full Version : Default selection of value in drop down list


simplyblue
09-05-2002, 10:12 AM
Hi,
Is there a way to default select a value in a drop down list in javascript?
I have a form which contains user information. It's for users to update their information. One of the field, country, is a drop-down list containing all the countries. Default selection has to be the value read from the database.
This is what I have,

<Select name=country >
<script>
document.form.country.options[<%=rs("ccountry")%>].selected='true'
</script>

<option value=AF >Afghanistan
<option value=AL >Albania
.......
</select>

But it doesn't work! Anyone can help? Thanks!

rsci
09-05-2002, 10:22 AM
I believe it is...

<option value=AF selected>Afghanistan
<option value=AL >Albania

glenngv
09-05-2002, 10:22 AM
<html>
<head>
<script>
function selectCountry(){
objSelect = document.yourFormNameHere.country;
for (var i=0;i<objSelect.options.length;i++){
if (objSelect.options[i].value=="<%=rs("ccountry")%>"){
objSelect.selectedIndex=i;
break;
}
}
}
</script>
</head>
<body onload="selectCountry()">
<form name="yourFormNameHere">
<Select name=country>
<option value="AF">Afghanistan
<option value="AL">Albania
</select>
</form>
</body>
</html>

simplyblue
09-05-2002, 10:46 AM
Thanks for your help glenn!
Finally got it to work :) :) :) :) :)