insertAdjacentText method
Creates a new TextNode from the specified text and inserts it at the specified position relative to the current element.
In Firefox, use the createTextNode, insertBefore and appendChild methods 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 TextNode needs to be inserted.
One of the following values:
|
|||||||||||||||||||||||||||||||||||||||
Required. String that specifies the contents of the TextNode to insert. |
Return value:
This method has no return value.
Example HTML code 1:
This example illustrates the use of the insertAdjacentText method:
|
||||
<head> <script type="text/javascript"> function InsertText (select) { var selected = select.selectedIndex; var where = select.options[selected].text; var relTo = document.getElementById ("relTo"); var textToInsert = "The position is " + where + "."; if (relTo.insertAdjacentText) { // Internet Explorer, Opera, Google Chrome and Safari relTo.insertAdjacentText (where, textToInsert); } 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 text relative to the destination element. <select onchange="InsertText (this);"> <option>beforeBegin</option> <option>afterBegin</option> <option>beforeEnd</option> <option>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 InsertText (select) { var selected = select.selectedIndex; var where = select.options[selected].text; var relTo = document.getElementById ("relTo"); var textToInsert = "The position is " + where + "."; if (relTo.insertAdjacentText) { // Internet Explorer, Opera, Google Chrome and Safari relTo.insertAdjacentText (where, textToInsert); } else { var textNodeToInsert = document.createTextNode (textToInsert); switch (where) { case "beforeBegin": relTo.parentNode.insertBefore (textNodeToInsert, relTo); break; case "afterBegin": relTo.insertBefore (textNodeToInsert, relTo.firstChild); break; case "beforeEnd": relTo.appendChild (textNodeToInsert); break; case "afterEnd": relTo.parentNode.insertBefore (textNodeToInsert, 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 text relative to the destination element. <select onchange="InsertText (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