Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.
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, Mgjbot, Ptak82, MarkFinkle, Pmash, Dria
 Last updated by: Sheppy,