ZVON > References > DOM2 Reference

add (method )

Owning interface and usage:  
HTMLSelectElement.add(element, before)

Member of these other interfaces:  
none

Description:  
Add a new element to the collection of OPTION elements for this SELECT. This method is the equivalent of the appendChild() method of the Node interface if the before parameter is null. It is equivalent to the insertBefore() method on the parent of before in all other cases. This method may have no effect if the new element is not an OPTION or an OPTGROUP.

Parameters:  
HTMLElement element  -  The element to add.
HTMLElement before  -  The element to insert before, or null for the tail of the list.

Returns:  
nothing

Exceptions:  
DOMException NOT_FOUND_ERR
Raised if before is not a descendant of the SELECT element.


Example:
Source:
   <SELECT id="component-select">
      <OPTION value="Component_1_a" selected="selected">Component_1</OPTION>
      <OPTION value="Component_1_b">Component_2</OPTION>
      <OPTION value="Component_2_a">Component_3</OPTION>
      <OPTION value="Component_2_b">Component_4</OPTION>
   </SELECT>
JavaScript:
  function addOption() {
    var el = document.createElement('OPTION');
    el.setAttribute('value', 'Component_3');
    el.appendChild( document.createTextNode('some_component_to_add') );
    var main = document.getElementById('component-select');
    main.add(el, null);
  }
Try it:  
See it: