insertAdjacentHTML method
Inserts a HTML formatted text at the specified position relative to the current element.
In Firefox, use the innerHTML property or the Range object to implement similar functionality.
See Example 2 for details.
Syntax:
You can find the related objects in the Supported by objects section below.
Parameters:
Required. String that specifies the position where the HTML formatted text needs to be inserted.
One of the following values:
|
|||||||||||||||||||||||||||||||||||||||
Required. String that specifies the HTML formatted text to insert. |
Return value:
This method has no return value.
Example HTML code 1:
This example illustrates the use of the insertAdjacentHTML method:
|
||||
<head> <script type="text/javascript"> function InsertHTML (select) { var selected = select.selectedIndex; var where = select.options[selected].text; var relTo = document.getElementById ("relTo"); var htmlToInsert = "The position is <b>" + where + "</b>."; if (relTo.insertAdjacentHTML) { // Internet Explorer, Opera, Google Chrome and Safari relTo.insertAdjacentHTML (where, htmlToInsert); } else { alert ("Your browser does not support the insertAdjacentHTML method!"); } } </script> </head> <body> <div id="relTo" style="width:300px; background:#e0d0a0;"> The destination element. </div> <br /><br /> Change the selected item to insert HTML formatted text relative to the destination element. <select onchange="InsertHTML (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 InsertHTML (select) { var selected = select.selectedIndex; var where = select.options[selected].text; var relTo = document.getElementById ("relTo"); var htmlToInsert = "The position is <b>" + where + "</b>."; if (relTo.insertAdjacentHTML) { // Internet Explorer, Opera, Google Chrome and Safari relTo.insertAdjacentHTML (where, htmlToInsert); } else { var range = document.createRange (); var docFragmentToInsert = range.createContextualFragment (htmlToInsert); switch (where) { case "beforeBegin": relTo.parentNode.insertBefore (docFragmentToInsert, relTo); break; case "afterBegin": relTo.insertBefore (docFragmentToInsert, relTo.firstChild); break; case "beforeEnd": relTo.appendChild (docFragmentToInsert); break; case "afterEnd": relTo.parentNode.insertBefore (docFragmentToInsert, relTo.nextSibling); break; } } } </script> </head> <body> <div id="relTo" style="width:300px; background:#e0d0a0;"> The destination element. </div> <br /><br /> Change the selected item to insert HTML formatted text relative to the destination element. <select onchange="InsertHTML (this);"> <option>beforeBegin</option> <option>afterBegin</option> <option>beforeEnd</option> <option>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