faqts : Computers : Programming : Languages : JavaScript : Forms : SELECT

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

6 of 15 people (40%) answered Yes
Recently 4 of 10 people (40%) answered Yes

Entry

How do I delete one Option in select box in IE ?

Feb 3rd, 2005 04:21
jsWalter, Guest,


This is what I use... 
 
This is part of a more extensive coolection of functions and methods 
for the SELECT Object I've collected/created over the past few years. 
 
Walter 
 
===================================== 
 
// {{{ _deleteOption() 
   /** 
    * Method private void _deleteOption( void ) 
    * 
    * New Method Member to the SELECT Form Object. 
    * This calls the public deleteOption() function. 
    * 
    * objSelectRef.deleteOption()  This will kill the selected item 
    * 
    * @name _deleteOption() 
    * @author Walter Torres <jsWalter@torres.ws> 
    * 
    * @category SELECT Object Extentions 
    * @uses deleteOption() 
    * 
    * @access private 
    * @since v1.0 
    * 
    * @param void 
    * @return void 
    * 
    */ 
    function _deleteOption() 
    { 
        deleteOption(this, this.selectedIndex); 
    }; 
// }}} 
// {{{ deleteOption() 
   /** 
    * Method public void deleteOption( object, int ) 
    * 
    * Delete given Item Index from given Select Object 
    * 
    * @name deleteOption 
    * @author Martin Webb <martin@irt.org> and Michel Plungjan 
<michel@irt.org> 
    * @copyright 1996-2001 irt.org, All Rights Reserved. 
    * @original http://developer.irt.org/script/916.htm 
    * 
    * @category SELECT Object Extentions 
    * 
    * @access public 
    * @since v1.0 
    * 
    * @param ref object Reference to Select Form Object 
    * @param int index Index of which Item to remove 
    * 
    */ 
    function deleteOption($objSelect, $intIndex) 
    { 
        // If $objSelect is a string, convert to Object Reference 
        // Then see if Object Reference exists 
        if ( ( typeof ( $objSelect ) ) == 'string' ) 
            $objSelect = document.getElementById($objSelect); 
 
        // If we have nothing to deal with, just bale... 
        if ( ( ! $objSelect ) ||               // Some browsers will 
define this 
             ( $objSelect == 'undefined' ) ||  // othes give this 
             ( $objSelect == '' ) ||           // Konqueror gives this 
             ( ! $intIndex ) )                 // Without an index, we 
have nothing to do 
            return; 
 
        $objSelect.options[$intIndex] = null; 
    }; 
// }}} 
// {{{ _deleteOptionAll() 
    function _deleteOptionAll() 
    { 
        // kill them all! 
        deleteOptionAll( this ); 
 
    }; 
// }}} 
// {{{ deleteOptionAll() 
    function deleteOptionAll( $objSelect ) 
    { 
        $objSelect.options.length = 0; 
    }; 
// }}} 
 
now "attach" these to the SELECT Object 
 
var $arySelectTags = document.getElementsByTagName("select"); 
 
    // Loop down all the SELECT Objects in this FORM and 
    // add new method members. 
    for (var $i = 0; $i < $arySelectTags.length ; $i++) 
    { 
       /** 
        * Insert Delete Selected Item 
        * 
        * @method public Deletes Selected Item 
        * @name deleteItem() 
        * @uses _deleteOption() 
        * 
        * @access public 
        * @since v1.4 
        */ 
        $arySelectTags[$i].deleteOption = _deleteOption; 
 
       /** 
        * Insert Delete All Options 
        * 
        * @method public Delete All Options 
        * @name deleteOptionAll() 
        * @uses _deleteOptionAll() 
        * 
        * @access public 
        * @since v1.4 
        */ 
        $arySelectTags[$i].deleteOptionAll = _deleteOptionAll 
 
    }   // for ... $arySelectTags.length