HTMLOptionsCollection Methods: add()

By: Dusty Arlia
Published on Monday, November 25, 2013, 07:21 AM
Last Updated on Saturday, July 11, 2015 at 12:46 AM
Total Updates: 3

HTMLOptionsCollection is a HTMLCollection. The add() method of the HTMLOptionsCollection JavaScript object, adds an element to the list of <option>'s in a <select> element. Here is the basic syntax:

ex_collection.add([element], [before])

Inserts the element before the second argument. The second argument is just an element in the HTMLCollection. The before argument can be the index number of the element to be inserted before or the element's name. If the before element is omitted, null, or a number out of range, then the element will be added at the end of the HTMLCollection. This method will throw a HierarchyRequestError exception if element is an ancestor of the element into which it is to be inserted.