MattsBits
MattsBits - Brunel District

MattsBits

MattsBits RSS Feed - Subscribe Now!

31/03/2010 : Remove Options From A Select List Using Javascript

Categories : | JavaScript | Views : 93 | Email Link Print

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>

Technorati Tags : | javascript | select list | options | remove | delete |

Author : Matt Hawkins  Last Edited By : Matt Hawkins  Permalink : Remove Options From A Select List Using Javascript

 
 
 
PHP Powered  MySQL Powered  Valid XHTML 1.0  Valid CSS  Firefox - Take Back The Web  EUKHost - Recommended Webhosting Solutions

MattHawkins CMS v2.0 - Copyright 2009-2010