mozilla
Your Search Results

    insertItemAt

    insertItemAt( index, label, value )
    Return type: element
    This method creates a new item and inserts it at the specified position. You may optionally set a value. The new item element is returned.

    Note: You cannot insert an item to an index that does not exist, eg: trying to insert an item at the end with element.getRowCount() + 1

    Example

    <!-- This example inserts at the selected item or appends, then selects the newly created item -->
    <script language="javascript">
    function insertItemToList(){
    
        var myListBox = document.getElementById('myListBox');
    
        // create a date to get some labels and values
        var someDate = new Date();
    
        if(myListBox.selectedIndex == -1){
            // no item was selected in list so append to the end
            myListBox.appendItem( someDate.toLocaleTimeString(), someDate.getTime() );
            var newIndex = myListBox.getRowCount()  -1
        }else{
            // item was selected so insert at the selected item
            var newIndex =  myListBox.selectedIndex;
            myListBox.insertItemAt(newIndex, someDate.toLocaleTimeString(), someDate.getTime());
        }
    
        // select the newly created item
        myListBox.selectedIndex = newIndex;
    }
    </script>
    
    <button label="Insert item at selected" oncommand="insertItemToList()"/>
    <listbox id="myListBox">
        <listitem label="foo"/>
    </listbox>
    

    See also

    Document Tags and Contributors

    Contributors to this page: Sheppy, trevorh, Marsf, Enn, MarkFinkle, Ptak82, Mgjbot, Dria, Pmash
    Last updated by: Sheppy,