insertAdjacentElement method
Inserts an element at the specified position relative to the current element.
If the element has a parent element, then it will be removed from the parent element before the insertion.
For a cross-browser solution, use the appendChild and insertBefore methods instead.
Syntax:
You can find the related objects in the Supported by objects section below.
Parameters:
Required. String that specifies the position where the element needs to be inserted.
One of the following values:
|
|||||||||||||||||||||||||||||||||||||||
Required. Reference to the element to insert. |
Return value:
Returns the inserted element.
Example HTML code 1:
This example illustrates the use of the insertAdjacentElement method:
|
||||
<head> <script type="text/javascript"> function Move (select) { var selected = select.selectedIndex; var where = select.options[selected].text; var relTo = document.getElementById ("relTo"); var wanderer = document.getElementById ("wanderer"); if (relTo.insertAdjacentElement) { // Internet Explorer, Opera, Google Chrome and Safari relTo.insertAdjacentElement (where, wanderer); } else { alert ("Your browser does not support the insertAdjacentElement method!"); } } </script> </head> <body> <div id="relTo" style="width:300px; height:50px; background:#e0d0a0;"> The destination element. </div> <div id="wanderer" style="color:green">The wanderer element.</div> <br /><br /> Change the selected item to move the wanderer element. <select onchange="Move (this);"> <option>beforeBegin</option> <option>afterBegin</option> <option>beforeEnd</option> <option selected="selected">afterEnd</option> </select> </body> |
||||
|
||||
Did you find this example helpful?
|
Example HTML code 2:
This example implements a cross-browser solution for the previous example:
|
||||
<head> <script type="text/javascript"> function Move (select) { var selected = select.selectedIndex; var where = select.options[selected].text; var relTo = document.getElementById ("relTo"); var wanderer = document.getElementById ("wanderer"); if (relTo.insertAdjacentElement) { // Internet Explorer, Opera, Google Chrome and Safari relTo.insertAdjacentElement (where, wanderer); } else { switch (where) { case "beforeBegin": relTo.parentNode.insertBefore (wanderer, relTo); break; case "afterBegin": relTo.insertBefore (wanderer, relTo.firstChild); break; case "beforeEnd": relTo.appendChild (wanderer); break; case "afterEnd": relTo.parentNode.insertBefore (wanderer, relTo.nextSibling); break; } } } </script> </head> <body> <div id="relTo" style="width:300px; height:50px; background:#e0d0a0;"> The destination element. </div> <div id="wanderer" style="color:green">The wanderer element.</div> <br /><br /> Change the selected item to move the wanderer element. <select onchange="Move (this);"> <option>beforeBegin</option> <option>afterBegin</option> <option>beforeEnd</option> <option selected="selected">afterEnd</option> </select> </body> |
||||
|
||||
Did you find this example helpful?
|
Supported by objects:
CommentNode
HTML elements:
a, abbr, acronym, address, applet, area, b, base, basefont, bdo, bgsound, big, blink, blockquote, body, br, button, caption, center, cite, code, col, colgroup, comment, dd, del, dfn, dir, div, dl, dt, em, embed, fieldset, font, form, frame, frameset, h1, h2, h3, h4, h5, h6, head, hr, html, i, iframe, img, input:button, input:checkbox, input:file, input:hidden, input:image, input:password, input:radio, input:range, input:reset, input:search, input:submit, input:text, ins, isindex, kbd, keygen, label, legend, li, link, listing, map, marquee, menu, meta, nobr, noframes, noscript, object, ol, optgroup, option, p, param, plaintext, pre, q, rt, ruby, s, samp, script, select, small, span, strike, strong, style, sub, sup, table, tbody, td, textarea, tfoot, th, thead, title, tr, tt, u, ul, var, wbr, xml, xmp
Related pages:
External links:
User Contributed Comments