Difference between innerText, innerHTML and value?

JavascriptDomInnerhtmlInnertext

Javascript Problem Overview


What is the difference between innerHTML, innerText and value in JavaScript?

Javascript Solutions


Solution 1 - Javascript

The examples below refer to the following HTML snippet:

<div id="test">
   Warning: This element contains <code>code</code> and <strong>strong language</strong>.
</div>

The node will be referenced by the following JavaScript:

var x = document.getElementById('test');


element.innerHTML

[Sets or gets the HTML syntax describing the element's descendants][moz-innerHTML]

x.innerHTML
// => "
// =>   Warning: This element contains <code>code</code> and <strong>strong language</strong>.
// => "

This is part of the W3C's [DOM Parsing and Serialization Specification][w3-innerHTML]. Note it's a property of Element objects.


node.innerText

[Sets or gets the text between the start and end tags of the object][msft-innerText]

x.innerText
// => "Warning: This element contains code and strong language."
  • innerText was introduced by Microsoft and was for a while unsupported by Firefox. In August of 2016, innerText was adopted by the WHATWG and was added to Firefox in v45.
  • innerText gives you a style-aware, representation of the text that tries to match what's rendered in by the browser this means:
    • innerText applies text-transform and white-space rules
    • innerText trims white space between lines and adds line breaks between items
    • innerText will not return text for invisible items
  • innerText will return textContent for elements that are never rendered like <style /> and `

Solution 2 - Javascript

Unlike innerText, though, innerHTML lets you work with HTML rich text and doesn't automatically encode and decode text. In other words, innerText retrieves and sets the content of the tag as plain text, whereas innerHTML retrieves and sets the content in HTML format.

Solution 3 - Javascript

InnerText property html-encodes the content, turning <p> to &lt;p&gt;, etc. If you want to insert HTML tags you need to use InnerHTML.

Solution 4 - Javascript

In simple words:

  1. innerText will show the value as is and ignores any HTML formatting which may be included.
  2. innerHTML will show the value and apply any HTML formatting.

Solution 5 - Javascript

Both innerText and innerHTML return internal part of an HTML element.

The only difference between innerText and innerHTML is that: innerText return HTML element (entire code) as a string and display HTML element on the screen (as HTML code), while innerHTML return only text content of the HTML element.

Look at the example below to understand better. Run the code below.

const ourstring = 'My name is <b class="name">Satish chandra Gupta</b>.';
document.getElementById('innertext').innerText = ourstring;
document.getElementById('innerhtml').innerHTML = ourstring;

.name {
    color:red;
}

<p><b>Inner text below. It inject string as it is into the element.</b></p>
<p id="innertext"></p>
<br>
<p><b>Inner html below. It renders the string into the element and treat as part of html document.</b></p>
<p id="innerhtml"></p>

Solution 6 - Javascript

var element = document.getElementById("main");
var values = element.childNodes[1].innerText;
alert('the value is:' + values);

To further refine it and retrieve the value Alec for example, use another .childNodes[1]

var element = document.getElementById("main");
var values = element.childNodes[1].childNodes[1].innerText;
alert('the value is:' + values);

Solution 7 - Javascript

In terms of MutationObservers, setting innerHTML generates a childList mutation due to the browsers removing the node and then adding a new node with the value of innerHTML.

If you set innerText, a characterData mutation is generated.

Solution 8 - Javascript

innerText property sets or returns the text content as plain text of the specified node, and all its descendants, whereas the innerHTML property gets and sets the plain text or HTML contents in the elements. Unlike innerText, innerHTML lets you work with HTML rich text and doesn’t automatically encode and decode text.

Solution 9 - Javascript

InnerText will only return the text value of the page with each element on a newline in plain text, while innerHTML will return the HTML content of everything inside the body tag, and childNodes will return a list of nodes, as the name suggests.

Solution 10 - Javascript

The innerText property returns the actual text value of an html element while the innerHTML returns the HTML content. Example below:

var element = document.getElementById('hello');
element.innerText = '<strong> hello world </strong>';
console.log('The innerText property will not parse the html tags as html tags but as normal text:\n' + element.innerText);

console.log('The innerHTML element property will encode the html tags found inside the text of the element:\n' + element.innerHTML);
element.innerHTML = '<strong> hello world </strong>';
console.log('The <strong> tag we put above has been parsed using the innerHTML property so the .innerText will not show them \n ' + element.innerText);
console.log(element.innerHTML);

<p id="hello"> Hello world 
</p>

Solution 11 - Javascript

To add to the list, innerText will keep your text-transform, innerHTML wont.

Solution 12 - Javascript

innerhtml will apply html codes

innertext will put content as text so if you have html tags it will show as text only

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser2819851View Question on Stackoverflow
Solution 1 - JavascriptfnyView Answer on Stackoverflow
Solution 2 - Javascriptalejo802View Answer on Stackoverflow
Solution 3 - JavascriptguymidView Answer on Stackoverflow
Solution 4 - JavascriptBalkishanView Answer on Stackoverflow
Solution 5 - JavascriptSatish Chandra GuptaView Answer on Stackoverflow
Solution 6 - Javascriptkaushik0033View Answer on Stackoverflow
Solution 7 - JavascriptNikosView Answer on Stackoverflow
Solution 8 - JavascriptVansh BhardwajView Answer on Stackoverflow
Solution 9 - Javascriptscrblnrd3View Answer on Stackoverflow
Solution 10 - JavascriptNickAthView Answer on Stackoverflow
Solution 11 - JavascriptTeiemView Answer on Stackoverflow
Solution 12 - JavascriptDaoudView Answer on Stackoverflow