Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org
#

Q1598 How can I pass text to a function and have it change the selection of a select box to the option which has that text as its value?

You are here: irt.org | FAQ | JavaScript | Form | Q1598 [ previous next ]

Try the following for a single select list:

<form name="myForm">
<select name="mySelect">
<option>abc
<option>xyz
<option>abc
<option>xyz
<option>abc
<option>xyz
<option>abc
<option>xyz
</select>
</form>

<script language="JavaScript"><!--
function selectIt(txt) {
  theSel = document.myForm.mySelect;
  for (i=0;i<theSel.options.length) {
    if (theSel.options[i].text == txt) {
      theSel.selectedIndex = i;
      break;
    }
  }
}
//--></script>

And the following for a multiple select list:

<form name="myForm">
<select name="mySelect" multiple size="3">
<option>abc
<option>xyz
<option>abc
<option>xyz
<option>abc
<option>xyz
<option>abc
<option>xyz
</select>
</form>

<script language="JavaScript"><!--
function selectIt(txt) {
  theSel = document.myForm.mySelect;
  for (i=0;i<theSel.options.length) {
    if (theSel.options[i].text == txt) {
      theSel.options[i].selected = true;
    }
  }
}
//--></script>

©2018 Martin Webb