Inserts a child into the node, immediately before the specified reference child node.
Syntaxvar insertedNode = node.insertBefore();
Parameters newNode
The new node to be inserted.
refChild(Optional)
Supplies the placement of the new node. If this parameter is specified, the new element will be inserted immediately before this existing child node. If not, it will be added after the last child node.
Return ValueReturns an object of type DOM NodeDOM Node
The inserted node.
ExamplesThe following example shows how to use the insertBefore method to insert a new item into an existing list.
<!doctype html>
<html>
<head>
<script type="application/javascript">
function insertElement()
{
var nod=document.createElement("li");
document.getElementById("oUL1").insertBefore(nod, document.getElementById("oLIYellow"));
nod.textContet="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>
</html>
Notes
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.
Related specificationsMozilla Developer Network : [Node.insertBefore Article]
Microsoft Developer Network: [insertBefore Method Article]
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4