Click to return to the DHTML, HTML     
insertAdjacentElement Met...     insertAdjacentText Method     DHTML Methods    
Web Workshop  |  DHTML, HTML & CSS

insertAdjacentHTML Method


Inserts the given HTML text into the element at the location.

Syntax

object.insertAdjacentHTML(sWhere, sText)

Parameters

sWhereRequired. String that specifies where to insert the HTML text, using one of the following values:
beforeBeginInserts sText immediately before the object.
afterBeginInserts sText after the start of the object but before all other content in the object.
beforeEndInserts sText immediately before the end of the object but after all other content in the object.
afterEndInserts sText immediately after the end of the object.
sTextRequired. String that specifies the 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.

Return Value

No return value.

Remarks

If the text contains HTML tags, the method parses and formats the text as it is inserted.

You cannot insert text while the document is loading. Wait for the onload event to fire before attempting to call this method.

As of Microsoft® Internet Explorer 5, this method is accessible at run time. If elements are removed at run time, before the closing tag is parsed, areas of the document might not render.

When using the insertAdjacentHTML method to insert script, you must include the DEFER attribute in the SCRIPT element.

Example

This example uses the insertAdjacentHTML method to insert script into the page.

Sample Code

var sHTML="<input type=button onclick=" + 
    "go2()" + " value='Click Me'><BR>"
var sScript='<SCRIPT DEFER>'
sScript = sScript + 
    'function go2(){ alert("Hello from inserted script.") }'
sScript = sScript + '</script' + '>';
ScriptDiv.insertAdjacentHTML("afterBegin",sHTML + sScript);

This feature requires Internet Explorer 4.0 or later. Click the icon below to install the latest version. Then reload this page to view the sample.
Microsoft Internet Explorer

Applies To

[ Object Name ]
PlatformVersion
Win16:
Win32:
Mac:
Unix:
WinCE:
A, ADDRESS, AREA, B, BASEFONT, BIG, BLOCKQUOTE, BODY, BUTTON, CAPTION, CENTER, CITE, CODE, COMMENT, custom, DD, DFN, DIR, DIV, DL, DT, EM, FIELDSET, FONT, FORM, FRAMESET, Hn, HR, I, IFRAME, IMG, INPUT type=button, INPUT type=checkbox, INPUT type=file, INPUT type=hidden, INPUT type=image, INPUT type=password, INPUT type=radio, INPUT type=reset, INPUT type=submit, INPUT type=text, KBD, LABEL, LEGEND, LI, LISTING, MAP, MARQUEE, MENU, NOBR, OL, OPTION, P, PLAINTEXT, PRE, S, SAMP, SELECT, SMALL, SPAN, STRIKE, STRONG, SUB, SUP, TD, TEXTAREA, TH, TT, U, UL, VAR, XMP

See Also

innerHTML, insertAdjacentText, outerHTML



Back to topBack to top

Did you find this topic useful? Suggestions for other topics? Write us!

© 1999 Microsoft Corporation. All rights reserved. Terms of use.