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

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

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

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

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>

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.