JavaScript Reference/Javascript Methods/insertBefore
"insertBefore()" Example
<source lang="javascript">
<html> <body> <script language="JavaScript"> function function1() {
var myElement = document.createElement("");document.all.myDiv.insertBefore(myElement);
} </script> <button id="myButton" onclick="function1();">Insert element</button>
</body> </html>
</source>
"insertBefore()" is applied to
<source lang="javascript">
+----------------+--------------------------------------------------------------+ | Applied_To |<a> <acronym> | | |<address> <applet> | | |<area> attribute | | | <base> | | |<basefont> |
| ||| |<body>
| | |<button>| | | | | |
| |<col> | | |<colgroup> <comment> | | |
| | | <dir> ||
| |
- | | |<fieldset> | | |<form> <frame> | | |<frameset> <head> | | |<hn>
|| |<html> | | |<iframe> <img> | | |<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"> | | |<isindex> | | |<label> <legend> |
| |- <link> | | |<listing> <map> | | |<marquee> <menu> | | |<meta> <noframes> | | |<noscript> <object> | | |
<optgroup> | | |<option>
| | |<param> <plaintext> | | |
| | || | |<script> <select> | | | | | || | |<style> | | || | |<tbody>
| | |<textarea> <tfoot> |
| |<thead> | | |<title> | | | | | | | | |<xmp> | +----------------+--------------------------------------------------------------+ </source>
"insertBefore()" Syntax, Parameters and Note
<source lang="javascript"> Note: Inserts an element either at the end of the document or after the specified element.
Syntax:
document.getElementById("elementID").insertBefore(param1, param2) document.all.elementID.insertBefore(param1, param2) // IE only
Parameters: param1 Required; the new element. param2 Optional; the element after which to insert the new element. If omitted, insert at the end.
</source>