Javascript Reference
Categories: Element

javascript Element insertAdjacentElement( )

@March 15, 2008, 1:22 a.m.
insertAdjacentElement( )Firefox/Netscape/NN n/a IE 5(Win) Chrome/Safari/DOM n/a  

insertAdjacentElement("where", elementObjectReference)

  

Inserts an element object into the designated position relative to the current element. Typically, the element object about to be inserted is created separately (for example, via document.createElement( )) or it may be a reference to an object already in the document tree, and the method essentially moves the object to its new location with the help of the insertAdjacentElement( ) method.

The destination is governed by the first attribute, which consists of one of four values that determine where the insertion occurs, as follows.

Although the effects on the document element tree are well-defined, the rendered result varies with the combination of inline and block elements you use as the current and inserted element objects. Inserting a block-level element (such as a div or p element) causes that element to render on the next line and at the left edge of the block-level positioning context (such as the body or td element). Applying the W3C DOM appendChild( ) method on elements is the equivalent of the insertAdjacentElement( ) method with the beforeEnd position parameter.

 
PositionInsert new element
BeforeBeginBefore start tag of current element, as a previous sibling
AfterBeginImmediately after current element's start tag, as a first child element
BeforeEndImmediately before current element's end tag, as a last child element
AfterEndAfter end tag of current element, as a next sibling
 
Parameters
 
  • String value of one of the following case-insensitive constants: BeforeBegin | AfterBegin | BeforeEnd | AfterEnd. The first and last locations are outside the HTML tags of the current element; the middle two locations are between the tags and element content.
  • Reference to any valid element object either existing in the document tree or created dynamically.
 
Returned Value

Reference to the inserted element object.

Under moderation.
Josephbiz - 2014-08-23

Powered by Linode.