« XUL Reference home
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


<!-- 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
        // 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;

<button label="Insert item at selected" oncommand="insertItemToList()"/>
<listbox id="myListBox">
    <listitem label="foo"/>

See also