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

Q1553 How can I pass a variable to a function and then sort through a select list to find and remove the option when it is found and also add the option if it is not found?

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

Try this:

<script language="JavaScript"><!--
function addOption(sText,sValue) {
  var found = false;
  for (i=0,n=document.myForm.mySelect.options.length;i<n;i++) {
    if (document.myForm.mySelect.options[i].value == sValue) {
      found = true;
      break;
    }
  }
  if (!found) {
    document.myForm.mySelect.options.length++; // first add one (Internet Explorer 5 fix)

    document.myForm.mySelect.options.[document.myForm.mySelect.options.length-1] = new Option(sText,sValue);
  }
  return !found; // returns true if insertion took place
}

if (addOption('Looking For','needle')) alert('New option added');
//--></script>

Feedback on 'Q1553 How can I pass a variable to a function and then sort through a select list to find and remove the option when it is found and also add the option if it is not found?'

©2018 Martin Webb