www.fgks.org   »   [go: up one dir, main page]

Click to Rate and Give Feedback
Methods
 add Method
add Method

Adds an element to the areas, controlRange, or options collection.

Syntax

object.add(oElement [, iIndex])

Parameters

oElement Required. Object that specifies the element to add to the collection.
iIndex Optional. 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.

Return Value

No return value.

Remarks

Before you can add an element to a collection, you must create it first by using the createElement method.

This method can be used to add elements only after the page loads.

If the method is applied inline, a run-time error occurs.

Example

This example uses the add method to insert an object into the options collection of a select object.

<SELECT ID="oSelect">
  <OPTION VALUE="1">One</OPTION>
</SELECT>

<SCRIPT>
var oOption = document.createElement("OPTION");
oSelect.options.add(oOption);
oOption.innerText = "Two";
oOption.value = "2";
</SCRIPT>

Standards Information

This method is defined in World Wide Web Consortium (W3C) Document Object Model (DOM) Level 1 World Wide Web link.

Applies To

areas, controlRange, options, SELECT

See Also

remove
Tags What's this?: Add a tag
Community Content
 
Add Community Content
Correction      bok470 ... John Sudds - MSFT   |   Edit   |  

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... :)]

© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker
DCSIMG