insertAdjacentHTML method

This topic has not yet been rated - Rate this topic

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

Syntax

object.insertAdjacentHTML(where, html)

Parameters

where [in]

Type: String

String that specifies where to insert the HTML text, using one of the following values:

beforeBegin

Inserts html immediately before the object.

afterBegin

Inserts html after the start of the object but before all other content in the object.

beforeEnd

Inserts html immediately before the end of the object but after all other content in the object.

afterEnd

Inserts html immediately after the end of the object.

html [in]

Type: String

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

Type: HRESULT

If this method succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code.

Standards information

There are no standards that apply here.

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.

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

Examples

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


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

See also

a
abbr
address
area
article
aside
b
bdo
blockQuote
body
br
button
caption
cite
code
col
colGroup
custom
dd
del
div
dl
dt
em
embed
fieldSet
figcaption
figure
footer
form
head
header
hgroup
hn
hr
html
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
ins
kbd
label
legend
li
map
mark
nav
object
ol
optGroup
option
p
pre
q
rt
ruby
s
samp
section
select
small
span
strong
sub
sup
table
tBody
td
textArea
tFoot
th
tHead
tr
u
ul
var
Reference
innerHTML
insertAdjacentText
outerHTML

 

 

Build date: 11/28/2012

Did you find this helpful?
(1500 characters remaining)
© 2013 Microsoft. All rights reserved.