ve*****@yahoo.com said the following on 4/5/2006 11:45 AM:
Say I have:
<select>
<option value='a' > First
<option value='b' > Second
<option value='c' > Third
</select>
Is there a way I can access the values First, Second and Third from
an array ? I want to write a javascript function that can
automatically select one option based on a regular expression match
of First Second or Third.
Get the .text property of the select. It will give you the First, Second
and Third that you are hunting.
So, loop through the select list checking it's .text property and then
when you find it, set the selectedIndex of the select list to the one
that corresponds to the .text property you want.
function setSelect(txt){
var sel = document.myForm.mySelect;
var selLength = sel.length;
var found = false;
for (var i=0;i<selLength;i++)
{
if (sel.options[i].text.toLowerCase() == txt.toLowerCase())
{
sel.options.selectedIndex = i;
found = true;
break;
}
}
if (!found){alert('That entry not found')}
}
With this HTML:
<form name="myForm">
<select name="mySelect">
<option value='a'>First
<option value='b'>Second
<option value='c'>Third
<option value='d'>Fourth
<option value='e'>Fifth
<option value='f'>Sixth
<option value='g'>Seventh
<option value='h'>Eighth
<option value='i'>Ninth
<option value='j'>Tenth
<option value='k'>Eleventh
<option value='l'>Twelfth
<option value='m'>Thirteenth
</select>
<input type="text" onchange="setSelect(this.value)">
</form>
Simply call setSelect with the text you want searched for.
--
Randy
comp.lang.javascript FAQ -
http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices -
http://www.JavascriptToolbox.com/bestpractices/