Click to See Complete Forum and Search --> : Select Option
amacfarl
10-03-2003, 07:56 PM
I have a drop down list box of countries and I want to set the selectedIndex of the box.
The only problem is that I dont know the index only the text, any ideas how I might achieve this?
Thanks
Angus
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en-US">
<head><title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript"><!--
function findSelIndex(selObj,findValue){
var msg = "Searched for text: "+findValue;
var isFound = false;
for(var i=0; i<selObj.options.length; i++){
if(selObj.options[i].text == findValue){
msg+="\nFound text. Option has the following properties:\nIndex:"+selObj.options[i].index+"\nValue:"+selObj.options[i].value;
isFound=true;
}
}
if(isFound==false){
msg+="\nNo text has been found, therefore no option exists with that text.";
}
alert(msg);
}
//--></script>
</head>
<body>
<form action="" name="myForm"><div>
<select size="1" name="mySel">
<option value="Number1">Text</option>
<option value="number2">Some text</option>
<option value="number3">Some more text</option>
<option value="number4">And so on</option>
</select><br>
<input type="text" name="searchFor" value="Option text to search for"><br>
<input type="button" value="Find Selectbox Index" onClick="findSelIndex(this.form.mySel,this.form.searchFor.value);">
</div></form>
</body></html>
[J]ona