asp.net.ph

Skip Navigation Links

insertBefore Method

DHTML Methods


DOM Level 1 Specification.

Inserts an element into the document hierarchy.

Syntax

object.insertBefore ( oNewNode [ ,  oRefChild ] ) 
Parameters

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.

Return Value

Returns a reference to the object that was inserted, if successful.

Remarks

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.

Example

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 

See Also

appendChild, createElement



© 2025 Reynald Nuñez and asp.net.ph. All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note