I have a hidden field which contains a value. I have a SELECT/OPTION list that has no selected options. After the select is written to the browser, I want to make to option, corresponding to the hidden value to become selected. The JavaScript below consistantly throws an undefined error when I execute the line to set the correct option's selected value to true. Funny enough, if I trap the error with a try .. catch block, the assignment works (cross browser).
Question: >How can I set the correct option to selected without the try .. catch block, without the error and without putting the options into an array and rebuilding the SELECT dynamically with JavaScript?
<select id="country" name="country" size="1">
<option value="AF">AFGHANISTAN</op
tion>
<option value="AL">ALBANIA</option
>
<option value="AD">ANDORRA</option
>
<option value="AO">ANGOLA</option>
<option value="AI">ANGUILLA</optio
n>
<option value="AG">ANTIGUA</option
>
<option value="VE">VENEZUELA</opti
on>
<option value="VN">VIETNAM</option
>
<option value="WF">WALLIS AND FUTUNA ISLANDS</option>
<option value="YE">YEMEN</option>
<option value="ZM">ZAMBIA</option>
<option value="ZW">ZIMBABWE</optio
n>
</select>
<input type="hidden" id="cSave" name="cSave" value="VN">
</form>
<script language="javascript" type="text/javascript">
<!--
if (document.getElementById('
cSave').va
lue!='') {
for (var idx=0;idx<document.getElem
entById('c
ountry').o
ptions.len
gth;idx++)
{
if (document.getElementById('
cSave').va
lue==docum
ent.getEle
mentById('
country').
options[id
x].value) {
document.getElementById('c
ountry').s
electedInd
ex=idx;
// The next line throws an undefined error.
// if the error is trapped with try .. catch, the option is selected
// as desired. I can live with the try .. catch since I get the
// action I need, but I would prefer to get the desired action
// without the try .. catch block, if possible.
document.getElementById('c
ountry').o
ptions[idx
].selected
=true;
// THIS WORKS BUT I WANT TO GET RID OF THE TRY .. CATCH
//try {
// document.getElementById('c
ountry').o
ptions[idx
].selected
=true;
//}
//catch(e) {
// if(e.error!=undefined) {
// alert(e.error);
// }
//}
break;
}
}
}
//-->
</script>
Thanks,
Rod