The XUL listbox provides a number of specialized methods.
List Manipulation
The listbox
The appendItem()
var el = env.locale; Example 1 : Source View
<script>
function addItem(){
  document.getElementById('thelist').appendItem("Thursday", "thu");
}
</script>
<listbox id="thelist"/>
<button label="Add" oncommand="addItem();"/>
The appendItem()label, in this case 'Thursday', and a value 'thu'. The two arguments correspond to the labelvaluelistitem
Similarly, there is also an insertItemAt()removeItemAt()
list.insertItemAt(3, "Thursday", "thu"); list.removeItemAt(3);
The insertItemAt()removeItemAt()
These three methods are also available for several other XUL elements and work in the same manner. In fact, these methods are part of the nsIDOMXULSelectControlElement interface so any XUL elements which implement this interface have these methods. This includes the menulistradiogrouptabs
List Selection
The nsIDOMXULSelectControlElement interface provides two additonal properties, selectedIndexselectedItemmenuitemselectedIndexselectedItem
Getting the selected item
These two properties are commonly inspected during a select event, as in the following example:
var el = env.locale; Example 2 : Source View
<listbox id="thelist" onselect="alert(this.selectedItem.label);"> <listitem label="Short"/> <listitem label="Medium"/> <listitem label="Tall"/> </listbox>
The select event is fired for a listbox when an item in the list is selected. The select handler here displays an alert containing the label of the selected item in the list. Since the select event fired we can assume that an item is selected. In other cases, you may wish to check to ensure that selectedItem is not null before continuing.
The select event is also fired when a radio button in a radiogrouptabsmenulists do not fire the select event; instead you can listen to the command event to handle when an item is selected.
For the tabstabboxselectedIndexselectedTabselectedPaneltab.
Changing the selection
All of the selection related properties described above may also be assigned a new value to change the selection. In the next example, the selectedIndexradiogroup
var el = env.locale; Example 3 : Source View
<script>
function doSelect(){
  var val = document.getElementById('number').value;
  val = Number(val);
  if (val != null)
    document.getElementById('level').selectedIndex = val - 1;
}
</script>
<hbox align="center">
  <label value="Enter a number from 1 to 3:"/>
  <textbox id="number"/>
  <button label="Select" oncommand="doSelect();"/>
</hbox>
<radiogroup id="level">
  <radio label="Excellent"/>
  <radio label="Good"/>
  <radio label="Poor"/>
</radiogroup>
Listboxes also support multiple selection and the functions of the nsIDOMXULMultiSelectControlElement interface. This interface provides a number of functions specifically for handling multiple selection. For example the selectedItemsselectedCount property holds the number of items selected. Typically, you will use these properties to iterate over the list and perform some operation for each item. Be careful when iterating over the selected items; if you modify the items in the list while iterating, the list will change and the selection properties may return different values. This is one reason why it is useful to manipulate the list by the item rather than by index.
Deleting selected items
The following example shows a method of deleting the selected items properly:
var el = env.locale; Example 4 : Source View
<script>
function deleteSelection(){
  var list = document.getElementById('thelist');
  var count = list.selectedCount;
  while (count--){
    var item = list.selectedItems[0];
    list.removeItemAt(list.getIndexOfItem(item));
  }
}
</script>
<button label="Delete" oncommand="deleteSelection();"/>
<listbox id="thelist" seltype="multiple">
  <listitem label="Cheddar"/>
  <listitem label="Cheshire"/>
  <listitem label="Edam"/>
  <listitem label="Gouda"/>
  <listitem label="Havartie"/>
</listbox>
Inside the while loop we
- first get the selectedItem at index 0. The first selected item is always retrieved as the size of the array will decrease as the items are removed.
- Next, we remove the item using the removeItemAt()
- we can convert between an item and an index using the getIndexOfItem()getItemAtIndex()
The nsIDOMXULMultiSelectControlElement interface also provides methods for modifying the selected items. For instance, the addItemToSelection()removeItemFromSelection()
List Scrolling
If there are more rows in the listbox
The scrollToIndex()ensureIndexIsVisible()ensureIndexIsVisible()
var el = env.locale; Example 5 : Source View
<button label="scrollToIndex"
           oncommand="document.getElementById('thelist').scrollToIndex(4);"/>
<button label="ensureIndexIsVisible"
           oncommand="document.getElementById('thelist').ensureIndexIsVisible(4);"/>
<listbox id="thelist" rows="5">
  <listitem label="1"/>
  <listitem label="2"/>
  <listitem label="3"/>
  <listitem label="4"/>
  <listitem label="5"/>
  <listitem label="6"/>
  <listitem label="7"/>
  <listitem label="8"/>
  <listitem label="9"/>
  <listitem label="10"/>
  <listitem label="11"/>
  <listitem label="12"/>
</listbox>
Next, we'll look at XUL box objects.