The To insert the HTML into the document rather than replace the contents of an element, use the method
A string containing the HTML serialization of the element's descendants. Setting the value of Thrown if an attempt
was made to set the value of Thrown if an attempt was made to insert the HTML into a node whose parent is a The Reading Element
property innerHTML
gets or sets the HTML or XML markup contained within the element. insertAdjacentHTML[]
. Value
innerHTML
removes all of the element's descendants and
replaces them with nodes constructed by parsing the HTML given in the string htmlString. Exceptions
SyntaxError
DOMException
innerHTML
using a string which is not properly-formed HTML. NoModificationAllowedError
DOMException
Document
. Usage notes
innerHTML
property can be used to examine the current HTML source of the page, including any changes that have been made since the page was initially loaded.Reading the HTML contents of an element
innerHTML
causes the user agent to serialize the HTML or XML fragment comprised of the element's descendants. The resulting string is returned.let contents = myElement.innerHTML;
This lets you look at the HTML markup of the element's content nodes.
Note: The returned HTML or XML fragment is generated based on the current contents of the element, so the markup and formatting of the returned fragment is likely not to match the original page markup.
Replacing the contents of an element
Setting the value of innerHTML
lets you easily replace the existing contents of an element with new content.
Note: This is a
security risk if the string to be inserted might contain potentially malicious content. When inserting user-supplied data you should always consider using Element.SetHTML[]
instead, in order to sanitize the content before it is inserted.
For example, you can erase the entire contents
of a document by clearing the contents of the document's body
attribute:
document.body.innerHTML = "";
This example fetches the document's current HTML markup and replaces the "