add method
[This documentation is preliminary and is subject to change.]
Adds an element to the areas, controlRange, or options collection.
![]() |
Syntax
object.add(element, before)Standards information
- Document Object Model (DOM) Level 2 HTML Specification, Section 1.6.5
Parameters
- element [in]
-
Type: IHTMLElement
Object that specifies the element to add to the collection. - before [in, optional]
-
Type: VARIANT
Variant of type Integer that specifies the index position in the collection where the element is placed. If no value is given, the method places the element at the end of the collection.
Internet Explorer 8 and later. Variant of type Object that specifies an element to insert before, or null to append the object to the collection.
Return value
Type: HRESULT
If this method succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code.
Type: HRESULT
If this method succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code.
Remarks
Internet Explorer 8. In IE8 Standards mode, before also accepts an Variant of type Object that specifies the object before which to insert the element. For more information, see Defining Document Compatibility.
This method adds an option element to a select block.
Before you can add an element to a collection, you must create it first by using the createElement method.
Examples
This example uses the add method to insert an object into the options collection of a select object. In Internet Explorer, the option element must be added to the collection prior to setting properties.
Code example: http://samples.msdn.microsoft.com/workshop/samples/author/dhtml/refs/add.htm
<select id="oSelect"> <option value="1">One</option> </select> <script type="text/javascript"> var oSelect = document.getElementById('oSelect'); var oOption = document.createElement('OPTION'); // Add the option to the collection first, then set properties oSelect.options.add(oOption); oOption.innerHTML = "Two"; oOption.value = "2"; </script>
Alternatively, you can achieve the same result with less code by using the array syntax of the options collection to append a new Option element.
Code example: http://samples.msdn.microsoft.com/workshop/samples/author/dhtml/refs/add.htm
<script type="text/javascript"> var opts = document.getElementById('oSelect').options; opts[opts.length] = new Option("Three","3"); </script>
In Internet Explorer 8 and later, the add method supports the standard way of specifying an object as an insertion point. The following code inserts the new Option in front of another option element already in the control.
Code example: http://samples.msdn.microsoft.com/workshop/samples/author/dhtml/refs/add.htm
<script type="text/javascript"> var sel = document.getElementById('oSelect'); sel.add(new Option("Four","4"), oOption); </script>
See also
Build date: 3/8/2012
--------
JScript Debugger
Breaking on JScript runtime error - Unspecified error
IE 9 (default Browser mode)
---------
Does not work and with no errors reported
IE 9 with Compatibility View or set to IE8 (Browser mode)
---------
SCRIPT16386: No such interface supported
//-----------Sample test code on Windows 7
var mylist = document.getElementById('myselectedItems'); //defined as <select name="myselectedItems" id="myselectedItems" multiple="false">
var objOption = document.createElement('option');
mylist.add(objOption); //Breaking on JScript runtime error - Unspecified error
//fails also with null as second parameter (tested working on Firefox 4, Chrome 5 per DOM standard)
objOption.text = 'newText';
objOption.value = 'newValue';
Any fix expected or is deployment of IE 10 a necessity to have this supported?
Thanks for supporting the community!
www.leedesmond.com
- 1/3/2012
- Desmond Lee
- 12/6/2011
- siva kondepati
page1:
<!-- blah blah blah ... -->
<select id="target">
</select>
<script language="javascript">
window.open("page2.htm");
</script>
page2:
<!-- blah blah blah ... -->
<script language="javascript">
var opnr = window.opener;
if ('undefined' != typeof(opnr)) {
var t = opnr.getElementById('target');
var opt = document.createElement('option');
opt.innerHTML = 'Foo';
opt.value = 'Bar';
t.options.add(opt); // IE8 just throw an Error (Unspecified Error)
}
</script>
Firefox works just fine
some one, please tell me why IE cannot do this.
thanks
- 11/30/2010
- Benjamin Wang
[EDIT] This is NOT true. You can create an option, set its value and text properties and then add it to a select without any problem. The problem is that you cannot alter the innerHTML property before adding it to a select (but you should be using the text property and not the innerHTML property anyway!)[/EDIT]
This does not work:
var oSelect = document.getElementById('mySelect'); var oOption = document.createElement('option');oOption.innerHTML = 'My new option';oOption.value = 42;oSelect.options.add(oOption);This does work:
var oSelect = document.getElementById('mySelect');var oOption = document.createElement('option');oSelect.options.add(oOption);oOption.innerHTML = 'My new option';oOption.value = 42;
- 12/12/2008
- Richard J. Turner
- 11/18/2010
- David.M.Reed
- 10/11/2008
- Jared Shields
the parameters for add(p1, p2) as defined by the ECMAScript group are:
HTMLElement, and HTMLElement (specifically options)
Further: this method is NOT compliant with the W3C DOM method, as indicated above. W3C also specifies the second argument to be a HTMLElement, described as " The element to insert before, or
null for the
tail of the list.". However if you try to supply null as the second argument to IE, you get a type mismatch error. See http://www.w3.org/TR/2000/WD-DOM-Level-1-20000929/level-one-html.html#ID-94282980
- 5/1/2008
- unique_username
- 5/26/2008
- harmic9186
For the example above,
oOption.innerText = "Two"; doesn't work
It should rather be
oOption.text = "Two";
[jsudds] Correction to the correction: innerText works, but is proprietary to Internet Explorer. If you're aiming for cross-browser, use the text property instead. [Oh, and getElementById(), too... :)]
- 1/19/2008
- bok470
- 1/19/2008
- John Sudds [Microsoft]
