Inserts an element into the document hierarchy as a child node of a parent object.
![]() |
Syntax
object.insertBefore(newChild, refChild)Parameters
- newChild [in]
-
Type: IHTMLDOMNode
IHTMLDOMNode that specifies the new object to be inserted.
- refChild [in, optional]
-
Type: Variant
Variant that specifies the placement of the new element. If this parameter is specified, the new element will be inserted immediately before this existing child element.
Return value
Type: IHTMLDOMNode
Returns a reference to the element that is inserted into the document.Standards information
Remarks
Do not specify the refChild parameter when inserting the first child node. If children already exist and you do not specify the refChild parameter, the newChild becomes the last child of the parent object.
This method is accessible at run time. If elements are removed at run time, before the closing tag has been parsed, areas of the document might not render.
Examples
The following example shows how to use the insertBefore method to insert a new item into an existing list.
<script type="text/javascript">
function insertElement()
{
var nod=document.createElement("li");
oUL1.insertBefore(nod, oLIYellow);
nod.innerText="Orange";
}
</script>
</head>
<body>
<p onclick="insertElement()">Click <strong>HERE</strong> to add an item to the following list.</p>
<ul id="oUL1">
<li id="oLIRed">Red</li>
<li id="oLIYellow">Yellow</li>
<li id="oLIBlue">Blue</li>
</ul>
</body>
See also
- a
- abbr
- address
- attribute
- b
- bdo
- blockQuote
- body
- button
- caption
- cite
- code
- col
- colGroup
- comment
- dd
- del
- div
- dl
- documentType
- dt
- em
- fieldSet
- form
- head
- hn
- html
- i
- iframe
- input type=button
- input type=checkbox
- input type=file
- input type=image
- input type=password
- input type=radio
- input type=reset
- input type=submit
- input type=text
- ins
- kbd
- label
- legend
- li
- map
- object
- ol
- option
- p
- pre
- ProcessingInstruction
- q
- s
- samp
- select
- small
- span
- strong
- sub
- sup
- table
- tBody
- td
- textArea
- tFoot
- th
- tHead
- tr
- u
- ul
- var
- About the W3C Document Object Model
Build date: 11/28/2012
