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

Q1730 How can I remove options from a select list based either on the text property value or the value property value?

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

Try:

<html>

<head>

<script language="JavaScript"><!--
function removeOptionsByText(selectName, text) {
  for (var i=selectName.options.length-1; i>=0; i--) {
    if (selectName.options[i].text == text) {
      selectName.options[i] = null;
    }
  }
}

function removeOptionsByValue(selectName, value) {
  for (var i=selectName.options.length-1; i>=0; i--) {
    if (selectName.options[i].value == value) {
      selectName.options[i] = null;
    }
  }
}
//--></script>

</head>

<body>

<form>

<select name="selectName">
<option value="fine">test
<option value="test">leave this alone
<option value="okay">test
<option value="xyz">test
<option value="test">okay
<option value="123">test
</select>

<input type="button" value="remove" onClick="removeOptionsByText(this.form.selectName,'test')">
<input type="button" value="remove" onClick="removeOptionsByValue(this.form.selectName,'test')">
</form>

</body>

</html>

©2018 Martin Webb