307

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

matronator
  • 123
  • 4
  • 9
user2819851
  • 3,121
  • 2
  • 11
  • 6
  • 4
    @tymeJV Honestly, the distinction with `innerText`, a non-standard implementation of textContext by MSIE, is non-trivial. – fny Sep 26 '13 at 14:26
  • 4
    In addition to innerText not working in Firefox: textContent seems to work in all major browsers, so just use textContent instead of innerText. – auco Jan 30 '15 at 11:32
  • 7
    IMPORTANT NOTE: The 3 comments above are no longer valid. `innerText` has been added to the standards and supported by all major browsers. `textContent` is now supported by IE>=9 and can be used instead of `innerText` in most cases (bonus, it is much faster), but there are differences between the two, so in some cases you cannot swap them. – Racil Hilan Feb 14 '18 at 06:08
  • 3
    Update 2019: `innerText` is well supported in all browsers. Firefox started supporting it from version 45. https://caniuse.com/#search=innertext – YetAnotherBot Jan 09 '19 at 06:21
  • I am surprised that security is not addressed here. `innerHTML` is a known vulnerability for XSS attacks. That said, `innerText` is not 100% secure either. https://stackoverflow.com/questions/52707031/does-innertext-prevent-xss https://blog.cloudboost.io/why-textcontent-is-better-than-innerhtml-and-innertext-9f8073eb9061 – davidhartman00 Apr 29 '20 at 20:15

12 Answers12

294

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

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. Note it's a property of Element objects.


node.innerText

Sets or gets the text between the start and end tags of the object

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 `
  • Property of Node elements


node.textContent

Gets or sets the text content of a node and its descendants.

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

While this is a W3C standard, it is not supported by IE < 9.

  • Is not aware of styling and will therefore return content hidden by CSS
  • Does not trigger a reflow (therefore more performant)
  • Property of Node elements


node.value

This one depends on the element that you've targeted. For the above example, x returns an HTMLDivElement object, which does not have a value property defined.

x.value // => null

Input tags (<input />), for example, do define a value property, which refers to the "current value in the control".

<input id="example-input" type="text" value="default" />
<script>
  document.getElementById('example-input').value //=> "default"
  // User changes input to "something"
  document.getElementById('example-input').value //=> "something"
</script>

From the docs:

Note: for certain input types the returned value might not match the value the user has entered. For example, if the user enters a non-numeric value into an <input type="number">, the returned value might be an empty string instead.


Sample Script

Here's an example which shows the output for the HTML presented above:

var properties = ['innerHTML', 'innerText', 'textContent', 'value'];

// Writes to textarea#output and console
function log(obj) {
  console.log(obj);
  var currValue = document.getElementById('output').value;
  document.getElementById('output').value = (currValue ? currValue + '\n' : '') + obj; 
}

// Logs property as [propName]value[/propertyName]
function logProperty(obj, property) {
  var value = obj[property];
  log('[' + property + ']'  +  value + '[/' + property + ']');
}

// Main
log('=============== ' + properties.join(' ') + ' ===============');
for (var i = 0; i < properties.length; i++) {
  logProperty(document.getElementById('test'), properties[i]);
}
<div id="test">
  Warning: This element contains <code>code</code> and <strong>strong language</strong>.
</div>
<textarea id="output" rows="12" cols="80" style="font-family: monospace;"></textarea>
Racil Hilan
  • 23,737
  • 12
  • 48
  • 51
fny
  • 27,890
  • 15
  • 90
  • 116
  • 1
    Even the most recent versions of Firefox do not support `innerText`: http://www.quirksmode.org/dom/html/ and http://www.quirksmode.org/dom/tests/innerhtml.html – Jarno Lamberg Oct 06 '14 at 15:00
  • 2
    It would be helpful to have each of the four properties (innerHTML, innerText, textContent, value) divided into two subheadings: "get" behavior and "set" behavior. – Luke Hutchison Jun 30 '15 at 00:09
  • I find getting innerText useful for GUI automation, so you can assert the text the user sees. – Ben Nieting Oct 18 '16 at 16:52
  • 3
    According to https://developer.mozilla.org/en-US/docs/Web/API/Node/innerText `Firefox >=45` is supported. – Kilmazing Jan 04 '17 at 17:45
  • 4
    If I understand the [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Node/innerText) correctly, `innerText` is now part of the Standard and should be supported by Firefox from version 45 on; maybe reason for an update to this great answer @faraz – domsson Jun 28 '17 at 10:04
  • 1
    It also converts `<` to ``, etc. – SarcasticSully Jan 10 '18 at 16:08
187

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.

Racil Hilan
  • 23,737
  • 12
  • 48
  • 51
alejo802
  • 2,037
  • 1
  • 12
  • 9
  • 11
    Very important to paste here in the accepted answer @jor's comment below in another answer: "Just for clearity: This only applies when SETTING a value. When you're GETTING the value HTML tags are simply stripped and you get the plain text." – Heitor Aug 07 '17 at 07:56
27

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.

galoget
  • 614
  • 9
  • 15
guymid
  • 1,166
  • 8
  • 10
  • 9
    Just for clearity: This only applies when SETTING a value. When you're GETTING the value HTML tags are simply stripped and you get the plain text. – jor Jul 18 '16 at 07:58
17

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.
Rohit Suthar
  • 3,483
  • 1
  • 38
  • 47
Balkishan
  • 271
  • 3
  • 4
11

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>
Satish Chandra Gupta
  • 1,932
  • 16
  • 22
3
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);
Gary
  • 12,206
  • 16
  • 46
  • 70
kaushik0033
  • 659
  • 6
  • 12
2

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.

galoget
  • 614
  • 9
  • 15
Nikos
  • 6,832
  • 7
  • 46
  • 81
2

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.

Fudge
  • 49
  • 4
Vansh Bhardwaj
  • 337
  • 2
  • 9
1

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.

galoget
  • 614
  • 9
  • 15
scrblnrd3
  • 6,725
  • 9
  • 32
  • 63
1

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>
NickAth
  • 1,001
  • 1
  • 8
  • 24
1

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

Fudge
  • 49
  • 4
Teiem
  • 804
  • 7
  • 21
0

innerhtml will apply html codes

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

Daoud
  • 36
  • 5