The innerHTML
property is used to set and get the HTML content within the element.
let htmlString = element.innerHTML;
element.innerHTML = htmlString;
When we set the innerHTML
of an element, all the child nodes of the element are
In the code above, we have created a div
and added some content to it.
We access the innerHTML
property of the div
element to get the HTML of the div
.
We set the value to the innerHTML
property of the div
element to set the HTML of the div
.
element.innerHTML = ""
This removes all the child nodes of the element.
document.body.innerHTML = "";
This erases the entire body of the webpage.
innerHTML
property, if the HTML String contains the &, <, >
character, then it will be replaced by the &
, <
, >
entities, respectively. & - &
< - <
> - >
<script>
tag inserted with innerHTML
will not execute.div.innerHTML = <script> alert(1) </script>
The code above will not be executed or added as a string to the div
.