DHTML Methods
DOM Level 1 Specification.
Inserts an element into the document hierarchy.
object.insertBefore ( oNewNode [ , oRefChild ] )
oNewNode |
Required. The address of the new node to be inserted. Elements can be created with the createElement method. |
oRefChild |
Optional. The address of the reference node. If specified, the new node will be inserted before this child node. If null , oNewNode is inserted at the end of the child list. |
Returns a reference to the object that was inserted, if successful.
This method inserts a child node immediately before the specified node or at the end of the list. This operation depends on the value of the nodeType property.
The oRefChild parameter should not be specified when inserting the first child node. If children already exist and the parameter is not specified, the new node will become the last child of the parent object.
The node supplied in oRefChild must be a child node of this node or null. The oNewNode is inserted before oRefChild ( as the left sibling ) in the child list. If oRefChild is null, oNewNode is inserted at the end of the child list. If oRefChild is not a child of this node, an error is returned.
When adding a document fragment, ie. if oNewNode is a node of type DOCUMENT_FRAGMENT, all its children are inserted, in the same order, before oRefChild. If oNewNode is already in the tree, it is first removed.
This sample demonstrates inserting a block of bold text into the document.
<head>
<script language="JavaScript">
function insertElement ( ) {
var nod=document.createElement ( "B" );
document.body.insertBefore ( nod );
nod.innerText="A new bold object has been inserted
into the document. "
}
</script>
</head>
<body>
<div onclick="insertElement ( )">Click here
to insert a new bold element into this div.</div>
</body>
Show me
appendChild, createElement