DHTML Methods
Inserts the given HTML text into the element at the location. If the text contains HTML tags, the method parses and formats the text as it is inserted.
object.insertAdjacentHTML ( sWhere, sText )
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. | |
sText |
Required. HTML text to insert. The string can be a combination of text and HTML tags. This must be well-formed, valid HTML or this method will fail. |
No return value.
You cannot insert text while the document is loading. Wait for the onload event before attempting to call this method.
When inserting script using the insertAdjacentHTML method you must include the DEFER attribute in the SCRIPT tag.
The following example shows use of the insertAdjacentHTML method to insert script into the page.
function insertScript ( ) {
if ( scriptDiv.innerHTML == "" ) {
var sHTML = "<input type=button onclick='sayHello ( ) ' value='Click Me'>";
var sScript = "<script defer>";
sScript += "function sayHello ( ) {";
sScript += "alert ( 'Hello. This message is from the inserted script.' ) }";
sScript += "</script>";
scriptDiv.insertAdjacentHTML ( "afterBegin", sHTML + sScript );
}
else alert ( 'The script is already available.' );
}
Show me
innerHTML, insertAdjacentText, outerHTML