DHTML Methods
Inserts an element at the specified location.
object.insertAdjacentElement ( sWhere, oElement )
sWhere |
Required. Specifies the position where insert the HTML text is to be inserted. This position can be one of the following:
beforeBegin |
Inserts the text immediately before the element. |
afterBegin |
Inserts the text after the start of the element but before all other content in the element. |
beforeEnd |
Inserts the text immediately before the end of the element but after all other content in the element. |
afterEnd |
Inserts the text immediately after the end of the element. | |
oElement |
Required. Element to be inserted adjacent to the object that invoked the insertAdjacentElement method. |
No return value.
The following example demonstrates the use of the insertAdjacentElement method to add a new list item to an OL object.
Sample Code
<script language="JavaScript">
function fnAdd ( ) {
var oNewItem = document.createElement ( "LI" );
oList.children ( 0 ).insertAdjacentElement (
"AfterEnd", oNewItem );
oNewItem.innerText = "List Item";
}
</script>
<body>
<ol id = "oList">
<li>List Item</li>
<li>List Item</li>
</ol>
<input type = "button" value = "Add Item"
onclick="fnAdd ( )">
<body>