Javascript Reference
Categories: options

javascript options add( )

@March 15, 2008, 1:22 a.m.
add( )Firefox/Netscape/NN n/a IE 4 Chrome/Safari/DOM n/a  

add(elementRef[, index])

  

Adds an already-created element (from the createElement( ) method) to the current collection. The element must be of the option type. By default, the new element is added as the last item of the collection unless you specify an index value as a second parameter (in which case all existing items from that index position get pushed down by one). The following example sequence appends a new item to a select object:

var newElem = document.createElement("option");
newElem.text = "Freddy";
newElem.value = "Freddy Mercury";
document.forms[1].rockers.options.add(newElem);
 

Notice that a generic object is created first. Then its properties are stuffed with values, and the new element is added to the select element.

 

For an example of a cross-browser and backward-compatible approach to this task, see the option object discussion. Also see the select.add( ) method for a W3C DOM approach that works with Netscape 6.

 
Parameters
 
  • A fully-formed element object reference, usually generated by the createElement( ) method.
  • An optional integer indicating where in the collection the new element should be placed.
 
Returned Value

None.

Under moderation.
WalterEnaph - 2018-01-21
Under moderation.
CraigMek - 2018-01-21
Under moderation.
Shawnneind - 2017-08-28

Powered by Linode.