What is the difference between innerHTML, innerText and value in JavaScript?
 
    
    - 465
- 6
- 13
 
    
    - 3,371
- 3
- 12
- 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
- 
                    4In 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
- 
                    8IMPORTANT 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
- 
                    4Update 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
- 
                    3I 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
14 Answers
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."
- innerTextwas introduced by Microsoft and was for a while unsupported by Firefox. In August of 2016,- innerTextwas adopted by the WHATWG and was added to Firefox in v45.
- innerTextgives you a style-aware, representation of the text that tries to match what's rendered in by the browser this means:- innerTextapplies- text-transformand- white-spacerules
- innerTexttrims white space between lines and adds line breaks between items
- innerTextwill not return text for invisible items
 
- innerTextwill return- textContentfor elements that are never rendered like- <style />and `
- Property of Nodeelements
 
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 Nodeelements
 
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> 
    
    - 24,690
- 13
- 50
- 55
 
    
    - 31,255
- 16
- 96
- 127
- 
                    1Even 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
- 
                    2It 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
- 
                    3According to https://developer.mozilla.org/en-US/docs/Web/API/Node/innerText `Firefox >=45` is supported. – Kilmazing Jan 04 '17 at 17:45
- 
                    4If 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
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.
 
    
    - 24,690
- 13
- 50
- 55
 
    
    - 2,107
- 1
- 12
- 9
- 
                    12Very 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
- 
                    Importantly, and perhaps unintuitively, setting an element's `innerText` with a multiline text value (containing CRLFs) will encode those CRLFs to `
 `'s. – Memetican Sep 08 '22 at 10:20
InnerText property html-encodes the content, turning <p> to <p>, etc. If you want to insert HTML tags you need to use InnerHTML.
- 
                    10Just 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
In simple words:
- innerTextwill show the value as is and ignores any- HTMLformatting which may be included.
- innerHTMLwill show the value and apply any- HTMLformatting.
 
    
    - 3,528
- 1
- 42
- 48
 
    
    - 281
- 3
- 4
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> 
    
    - 2,970
- 1
- 22
- 22
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);
 
    
    - 13,303
- 18
- 49
- 71
 
    
    - 679
- 6
- 12
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.
 
    
    - 49
- 4
 
    
    - 427
- 5
- 9
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> 
    
    - 1,089
- 1
- 14
- 35
@rule:
innerHTML
- write: whatever String you write to the - ele.innerHTML,- ele(the code of the element in the html file) will be exactly same as it is written in the String.
- read : whatever you read from the - ele.innerHTMLto a String, the String will be exactly same as it is in- ele(the html file).
- => .innerHTMLwill not make any modification for your read/write
innerText
- write: when you write a String to the - ele.innerText, any- html reserved special characterin the String will be encoded into html format first, then stored into the- ele.- eg: <p>in your String will become<p>in theele
 
- eg: 
- read : when you read from the - ele.innerTextto a String,- any - html reserved special characterin the- elewill be decoded back into a readable text format,- eg: <p>in theelewill become back into<p>in your String
 
- eg: 
- any (valid) - html tagin the- elewill be removed -- so it becomes "plain text"- eg: if <em>you</em> canin theelewill becomeif you canin your String
 - about invalid - html tag- if there is an invalid - html tagoriginally in the- ele(the html code), and you read from- .innerText, how does the tag gets removed?- -- this ("if there is an invalid - html tagoriginally") should not (is not possible to) happen
- but its possible that you write an invalid - html tagby- .innerHTML(in raw) into- ele-- then, this may be auto fixed by the browser.
 
 - dont take (-interpret) this as step [1.] [2.] with an order -- no, take it as step [1.] [2.] are executed at the same time - -- I mean, if the decoded characters in [1.] will form a new tag after the conversion, [2.] does not remove it - (-- cuz [2.] considers what characters are in the - eleduring the conversion, not the characters they become into after the conversion)
 
- eg: 
- then stored into the String. 
 
- (^ this contains much more explanations in comments of the js file, + output in console.log - below is a simplified view, with some output. - (try out the code yourself, also there is no guarantee that my explanations are 100% correct.)) 
<p id="mainContent">This is a <strong>sample</strong> sentennce for Reading.</p>
<p id="htmlWrite"></p>
<p id="textWrite"></p>
// > @basic (simple)
// read
var ele_mainContent = document.getElementById('mainContent');
alert(ele_mainContent.innerHTML); // This is a <strong>sample</strong> sentennce for Reading.        // >" + => `.innerHTML` will **not make any modification** for your read/write
alert(ele_mainContent.innerText); // This is a sample sentennce for Reading.                         // >" 2. any (valid) `html tag` in the `ele` will be **removed** -- so it becomes "plain text"
// write
var str_WriteOutput = "Write <strong>this</strong> sentence to the output.";
var ele_htmlWrite = document.getElementById('htmlWrite');
var ele_textWrite = document.getElementById('textWrite');
ele_htmlWrite.innerHTML = str_WriteOutput;
ele_textWrite.innerText = str_WriteOutput;
alert(ele_htmlWrite.innerHTML); // Write <strong>this</strong> sentence to the output.               // >" + => `.innerHTML` will **not make any modification** for your read/write
alert(ele_htmlWrite.innerText); // Write this sentence to the output.                                // >" 2. any (valid) `html tag` in the `ele` will be **removed** -- so it becomes "plain text"                     
alert(ele_textWrite.innerHTML); // Write <strong>this</strong> sentence to the output.   // >" any `html reserved special character` in the String will be **encoded** into html format first               
alert(ele_textWrite.innerText); // Write <strong>this</strong> sentence to the output.               // >" 1. any `html reserved special character` in the `ele` will be **decoded** back into a readable text format,  
// > @basic (more)
// write - with html encoded char
var str_WriteOutput_encodedChar = "What if you have <strong>encoded</strong> char in <strong>the</strong> sentence?";
var ele_htmlWrite_encodedChar = document.getElementById('htmlWrite_encodedChar');
var ele_textWrite_encodedChar = document.getElementById('textWrite_encodedChar');
ele_htmlWrite_encodedChar.innerHTML = str_WriteOutput_encodedChar;
ele_textWrite_encodedChar.innerText = str_WriteOutput_encodedChar;
alert(ele_htmlWrite_encodedChar.innerHTML); // What if you have <strong>encoded</strong> char in <strong>the</strong> sentence?
alert(ele_htmlWrite_encodedChar.innerText); // What if you have <strong>encoded</strong> char in the sentence?
alert(ele_textWrite_encodedChar.innerHTML); // What if you have &lt;strong&gt;encoded&lt;/strong&gt; char in <strong>the</strong> sentence?
alert(ele_textWrite_encodedChar.innerText); // What if you have <strong>encoded</strong> char in <strong>the</strong> sentence?
// > @note-advance: read then write 
var ele__htmlRead_Then_htmlWrite = document.getElementById('htmlRead_Then_htmlWrite');
var ele__htmlRead_Then_textWrite = document.getElementById('htmlRead_Then_textWrite');
var ele__textRead_Then_htmlWrite = document.getElementById('textRead_Then_htmlWrite');
var ele__textRead_Then_textWrite = document.getElementById('textRead_Then_textWrite');
ele__htmlRead_Then_htmlWrite.innerHTML = ele_mainContent.innerHTML;
ele__htmlRead_Then_textWrite.innerText = ele_mainContent.innerHTML;
ele__textRead_Then_htmlWrite.innerHTML = ele_mainContent.innerText;
ele__textRead_Then_textWrite.innerText = ele_mainContent.innerText;
alert(ele__htmlRead_Then_htmlWrite.innerHTML); // This is a <strong>sample</strong> sentennce for Reading.
alert(ele__htmlRead_Then_htmlWrite.innerText); // This is a sample sentennce for Reading.
alert(ele__htmlRead_Then_textWrite.innerHTML); // This is a <strong>sample</strong> sentennce for Reading.
alert(ele__htmlRead_Then_textWrite.innerText); // This is a <strong>sample</strong> sentennce for Reading.
alert(ele__textRead_Then_htmlWrite.innerHTML); // This is a sample sentennce for Reading.
alert(ele__textRead_Then_htmlWrite.innerText); // This is a sample sentennce for Reading.
alert(ele__textRead_Then_textWrite.innerHTML); // This is a sample sentennce for Reading.
alert(ele__textRead_Then_textWrite.innerText); // This is a sample sentennce for Reading.
// the parsed html after js is executed
/*
<html><head>
<meta charset="utf-8">
<title>Test</title>
</head>
<body>
<p id="mainContent">This is a <strong>sample</strong> sentennce for Reading.</p>
<p id="htmlWrite">Write <strong>this</strong> sentence to the output.</p>
<p id="textWrite">Write <strong>this</strong> sentence to the output.</p>
<!-- P2 -->
<p id="htmlWrite_encodedChar">What if you have <strong>encoded</strong> char in <strong>the</strong> sentence?</p>
<p id="textWrite_encodedChar">What if you have &lt;strong&gt;encoded&lt;/strong&gt; char in <strong>the</strong> sentence?</p>
<!-- P3 @note: -->
<p id="htmlRead_Then_htmlWrite">This is a <strong>sample</strong> sentennce for Reading.</p>
<p id="htmlRead_Then_textWrite">This is a <strong>sample</strong> sentennce for Reading.</p>
<p id="textRead_Then_htmlWrite">This is a sample sentennce for Reading.</p>
<p id="textRead_Then_textWrite">This is a sample sentennce for Reading.</p>
</body></html>
*/
 
    
    - 555
- 1
- 5
- 13
innerhtml will apply html codes
innertext will put content as text so if you have html tags it will show as text only
 
    
    - 36
- 1
- 5
1)innerHtml
- sets all the html content inside the tag
- returns all the html content inside the tag
- includes styling + whitespaces
2)innerText
- sets all the content inside the tag (with tag wise line breaks)
- returns all html content inside the tag (with tag wise line breaks)
- ignores tags (shows only text)
- ignores styling + whitespaces
- if we have style:"visibility:hidden;" inside tag |_ innerText includes the styling -> hides content
3)textContent
- sets all the content inside the tag (no tag wise line breaks)
- returns all content inside the tag (no tag wise line breaks)
- includes whitespaces
- if we have style:"visibility:hidden;" inside tag |_ textContent ignores the styling -> shows content
- textContent has better performance because its value is not parsed as HTML.
   
 
    
    - 2,203
- 1
- 14
- 12
 
     
     
     
     
    


