Difference between innerHTML, outerHTML, innerText, outerText in Javascript
innerHTML, outerHTML, innerText, outerText are the HTML Element's property which we can use in JavaScript to modify the Html Content and text of an HTML element. innerHTML and outerHTML helps to modify the HTML content of an element whereas innerText and outerText helps to modify the text of an element.
innerHTML: innerHTML property in JavaScript helps to get the entire HTML content of an element and also helps to replace the entire HTML content inside of an element as shown below:
Below HTML we have a div with id "innerHtmlElement" which has <h4> tag and in the page we have "Change Content" button, on click of it we are going to change the innerHTML of "innerHtmlElement" div.
- <html>
- <head><title>Test Application</title></head>
- <body style="text-align:center;margin:20px;">
- <div id="innerHtmlElement"><h4>Inner HTML Element</h4></div>
- <div><input type="button" onclick="ChangeContent();" value="Change Content"/></div>
- <script>
- var innerHtmlElm = document.getElementById('innerHtmlElement');
- function ChangeContent(){
- console.log('Old Content: '+innerHtmlElm.innerHTML);
- innerHtmlElm.innerHTML='<h4>Inner HTML Element Content is Changed</h4>';
- }
- </script>
- </body>
- </html>
After clicking on "Change Content" button, the innerHTML of the "div with id innerHtmlElement" is replaced with the new HTML code, as shown below it got replaced with new <h5> tag and it will display on page with new h5 heading style
- <html>
- <head><title>innerHTML Test Application</title></head>
- <body style="text-align:center;margin:20px;">
- <div id="innerHtmlElement"><h5>Inner HTML Element Content is Changed</h5></div>
- <div><input type="button" onclick="ChangeContent();" value="Change Content"></div>
- <script>
- var innerHtmlElm = document.getElementById('innerHtmlElement');
- function ChangeContent(){
- console.log('Old Content: '+innerHtmlElm.innerHTML); //outputs: Old Content: <h4>Inner HTML Element</h4>
- innerHtmlElm.innerHTML='<h5>Inner HTML Element Content is Changed</h5>';
- }
- </script>
- </body>
- </html>
You can see with innerHTML, line number 4 content is changed, below is the output shown after changing the innerHTML
outerHTML: outerHTML helps to replace the element with the new HTML element, it will render the element and give it's default styling to the contents inside the new element.
As shown below, on click on "Change Content" button, we are going to change the outerHTML of a div with id "outerHtmlElement":
- <html>
- <head><title>outerHTML Test Application</title></head>
- <body style="text-align:center;margin:20px;">
- <div id="outerHtmlElement"><h4>Outer HTML Element</h4></div>
- <div><input type="button" onclick="ChangeContent();" value="Change Content"></div>
- <script>
- var outerHtmlElm = document.getElementById('outerHtmlElement');
- function ChangeContent(){
- console.log('Old Content: '+outerHtmlElm.innerHTML);
- outerHtmlElm.outerHTML='<h5>Outer HTML Element Content is Changed</h5>';
- }
- </script>
- </body></html>
After clicking on "Change Content" button, it replaced the "div#outerHtmlElement" with new "<h5>" tag and it will apply the h5 heading styles to the content inside the <h5> tag, now on the page we don't have any div element with id as "outerHtmlElement" because we replaced it with new <h5> tag by using outerHTML property.
- <html>
- <head><title>outerHTML Test Application</title></head>
- <body style="text-align:center;margin:20px;">
- <h5>Outer HTML Element Content is Changed</h5>
- <div><input type="button" onclick="ChangeContent();" value="Change Content"></div>
- <script>
- var outerHtmlElm = document.getElementById('outerHtmlElement');
- function ChangeContent(){
- console.log('Old Content: '+outerHtmlElm.innerHTML);
- outerHtmlElm.outerHTML='<h5>Outer HTML Element Content is Changed</h5>';
- }
- </script>
- </body></html>
See the below output, when we changed the HTML content using outerHTML:
innerText: innerText helps to set the replace the entire inside HTML content of any element with the new text, it will render the text as it is. even if you specify any tag as text, it will render as it is on the browser without any styles.
As shown in the below HTML, JavaScript code, We are going to apply the innerText to "div#innerTextHtmlElement":
- <html>
- <head><title>innerText Test Application</title></head>
- <body style="text-align:center;margin:20px;">
- <div id="innerTextHtmlElement"><h4>innerText HTML Element</h4></div>
- <div><input type="button" onclick="ChangeContent();" value="Change Content"/></div>
- <script>
- var innerTextElm = document.getElementById('innerTextHtmlElement');
- function ChangeContent(){
- console.log('Old Content: '+innerTextElm.innerHTML);
- innerTextElm.innerText = '<h5>InnerText HTML Element Content is Changed</h5>';
- }
- </script>
- </body>
- </html>
After clicking on "Change Content" button, the entire content of "div#innerTextHtmlElement" is replaced with the new text. Even if we put any HTML tag in the new text, it will just show the tag on the browser as it is in plain text without any styling.
- <html>
- <head><title>innerText Test Application</title></head>
- <body style="text-align:center;margin:20px;">
- <div id="innerTextHtmlElement"><h5>Inner HTML Element Content is Changed</h5></div>
- <div><input type="button" onclick="ChangeContent();" value="Change Content"></div>
- <script>
- var innerTextElm = document.getElementById('innerTextHtmlElement');
- function ChangeContent(){
- console.log('Old Content: '+innerTextElm.innerHTML);
- innerTextElm.innerText = '<h5>InnerText HTML Element Content is Changed</h5>';
- }
- </script>
- </body></html>
We can see in the below HTML output page, the new text is rendered as it is, it didn't apply any stying to the new content of the "div#innerTextHtmlElement":
outerText: outerText replaces an element with the new text content passed to outerText property. As shown in the below HTML code, we are going to change the outerText of a div element with id "outerTextHtmlElement"
- <html>
- <head><title>outerText Test Application</title></head>
- <body style="text-align:center;margin:20px;">
- <div id="outerTextHtmlElement"><h4>outerText HTML Element</h4></div>
- <div><input type="button" onclick="ChangeContent();" value="Change Content"/></div>
- <script>
- var outerTextElm = document.getElementById('outerTextHtmlElement');
- function ChangeContent(){
- console.log('Old Content: '+outerTextElm.outerText);
- outerTextElm.outerText = '<h5>OuterText HTML Element Content is Changed</h5>';
- }
- </script>
- </body>
- </html>
After clicking on "Change Content" button, it replaced the "div#outerTextHtmlElement" with new text, even if we put <h5> tag in the new text to be replaced, it displayed as it is in the plain text on the browser. Now on the HTML page, we don't have any div with id "outerTextHtmlElement" because we replaced it with the new text by using "outerText" property
- <html>
- <head><title>outerText Test Application</title></head>
- <body style="text-align:center;margin:20px;">
- " <h5>OuterText HTML Element Content is Changed</h5> "
- <div><input type="button" onclick="ChangeContent();" value="Change Content"></div>
- <script>
- var outerTextElm = document.getElementById('outerTextHtmlElement');
- function ChangeContent(){
- console.log('Old Content: '+outerTextElm.outerText);
- outerTextElm.outerText = '<h5>OuterText HTML Element Content is Changed</h5>';
- }
- </script>
- </body>
- </html>
As shown below, the "div#outerTextHtmlElement" is replaced with new text and it is shown as it is on the browser without applying any styles to it: