MattsBits
MattsBits

MattsBits

Remove Options From A Select List Using Javascript  

by Matt Hawkins, 31/03/2010
Categories : JavaScript

Sometimes you need to remove options from an HTML select list using Javascript. The following function can be called to remove options from a select list given the value of the relevant option.

The function requires two parameters. The first is the value of the option to remove and the second is the select list reference.

In order to get the correct reference you can use the following code :

myRef = document.getElementById('mySelectList');


Where mySelectList is the ID of your select list.

The function is defined using :


<script language="JavaScript">
<!-- Begin
function SelectOptionRemove(pValue, pid)
{
// This function removes an option
// from an HTML select list
//
// pValue - Value of option to remove
// pID - select list to remove
//
// http://www.mattsbits.co.uk

var i;
var vLen;

// Get number of options in select list
vLen = pid.options.length;

// Loop through each option
for (i=0;i<vLen ;i++ )
{
// Look for match
if (pid.options[i].value == pValue)
{
// Remove option and exit loop
pid.options[i] = null;
break;
}
}
}
// End -->
</script>

Author : Matt Hawkins  Last Edit By : Matt Hawkins
PHP Powered  MySQL Powered  Valid XHTML 1.0  Valid CSS  Firefox - Take Back The Web  EUKHost - Recommended Webhosting Solutions

MattHawkins CMS v3.0 - Copyright 2009-2022