Home Articles FAQs XREF Games Software Instant Books About Feedback Search Site-Map
irt.org logo

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?

irt.org | Knowledge Base | JavaScript | Form | Q1553 [ previous next ]

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?

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?'


Provide feedback ...
AddThis Social Bookmark Button

Provide feedback ... AddThis Social Bookmark Button


Last Updated: 30th March 2008. Maintained by: Martin Webb and Michel Plungjan
irt.org liability, trademark, document use, privacy statement and software licensing rules apply.
Copyright © 1996-2008 irt.org, All Rights Reserved.