171

I have a dropdown list which is populated by Javascript.

Whilst deciding what should be the default value to show on load, I realised that the following properties showed exactly the same values:

  • innerText
  • innerHTML
  • label
  • text
  • textContent
  • outerText

My own research shows bench marking tests or comparisons between a few of them, but not all.

I can use my own common sense and choose 1 or the other as they provide the same result, but, I'm concerned this is not going to be a good idea if the data were to change.

My findings are:

  • innerText will show the value as is and ignores any HTML formatting which may be included
  • innerHTML will show the value and apply any HTML formatting
  • label appears to be the same as innerText, so I can't see the difference
  • text appears to be the same as innerText but the jQuery shorthand version
  • textContent appears to the same as innerText but keeps formatting (such as \n)
  • outerText appears to be the same as innerText

My research can only take me so far as I can only test what I can think of or read what is published, can any one confirm though if my research is correct and if there is anything special about label and outerText?

Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
MyDaftQuestions
  • 4,487
  • 17
  • 63
  • 120
  • 13
    One of the reasons there are so many different ways to access the text is because of cross-browser differences. If you are already using jQuery, you should use `.text()` to get an element's text content, since that will provide maximal cross-browser support. – JLRishe Jun 26 '14 at 10:17
  • 2
    https://developer.mozilla.org/en-US/docs/Web/API/Node.textContent#Differences_from_innerText – JLRishe Jun 26 '14 at 10:19
  • MDN docs: [`innerText`](//developer.mozilla.org/docs/Web/API/HTMLElement/innerText), [`innerHTML`](//developer.mozilla.org/docs/Web/API/Element/innerHTML) ([for `ShadowRoot`s](//developer.mozilla.org/docs/Web/API/ShadowRoot/innerHTML)), [`label` and `text`](//developer.mozilla.org/docs/Web/API/HTMLOptionElement#properties) (`label` and `text` also exist on lots of Media-related prototypes), [`textContent`](//developer.mozilla.org/docs/Web/API/Node/textContent), [`outerText`](//developer.mozilla.org/docs/Web/API/HTMLElement/outerText). – Sebastian Simon Dec 16 '21 at 17:06
  • Also relevant: [`outerHTML`](//developer.mozilla.org/docs/Web/API/Element/outerHTML), [`nodeValue`](//developer.mozilla.org/docs/Web/API/Node/nodeValue), [`wholeText`](//developer.mozilla.org/docs/Web/API/Text/wholeText). – Sebastian Simon Dec 16 '21 at 17:06

6 Answers6

140

From MDN:

Internet Explorer introduced element.innerText. The intention is pretty much the same [as textContent] with a couple of differences:

  • Note that while textContent gets the content of all elements, including <script> and <style> elements, the mostly equivalent IE-specific property, innerText, does not.

  • innerText is also aware of style and will not return the text of hidden elements, whereas textContent will.

  • As innerText is aware of CSS styling, it will trigger a reflow, whereas textContent will not.

So innerText will not include text that is hidden by CSS, but textContent will.

innerHTML returns the HTML as its name indicates. Quite often, in order to retrieve or write text within an element, people use innerHTML. textContent should be used instead. Because the text is not parsed as HTML, it's likely to have better performance. Moreover, this avoids an XSS attack vector.

In case you missed that, let me repeat it more clearly: Do not use .innerHTML unless you specifically intend to insert HTML within an element and have taken the necessary precautions to ensure that the HTML you are inserting cannot contain malicious content. If you only want to insert text, use .textContent or if you need to support IE8 and earlier, use feature detection to switch off between .textContent and .innerText.

A main reason that there are so many different properties is that different browsers originally had different names for these properties, and there still isn't complete cross-browser support for all of them. If you are using jQuery, you should stick to .text() since that is designed to smooth out cross-browser differences.*

For some of the others: outerHTML is basically the same as innerHTML, except that it includes the start and end tags of the element it belongs to. I can't seem to find much description of outerText at all. I think that is probably an obscure legacy property and should be avoided.

Community
  • 1
  • 1
JLRishe
  • 99,490
  • 19
  • 131
  • 169
  • 3
    The problem with that doc's advice, `in order to retrieve or write text within an element, people use innerHTML. textContent should be used instead`, is that **textContent** is not supported by IE 8, which is still in fairly widespread use because that's the version bundled with Windows 7. And FireFox doesn't support **innerText**. So while **innerHTML** isn't ideally suited for the purpose, it has better cross-browser reliability. – Adi Inbar May 08 '15 at 01:10
  • 5
    @AdiInbar If you need to support old browsers, the correct thing to do is to use feature detection to switch off between `.textContent` and `.innerText`, or to use something like jQuery that smooths out these browser differences. – JLRishe May 08 '15 at 05:11
  • 1
    Thank you so much for this explanation! I am using flot to display charts and it wasn't showing in ff because ff uses textContent. Innertext was always undefined when I used the tickFormatter. – Rainhider Jul 20 '15 at 19:29
  • But how can I use `textContent` with HTML tags? – Bob van Luijt Sep 02 '15 at 10:34
  • 1
    @bvl You can't use `textContent` with HTML tags. If you need to insert HTML, you would either use `.innerHTML` or build up HTML nodes using `document.createElement()`, etc. – JLRishe Sep 02 '15 at 14:34
  • 6
    **Security:** Incorrect usage of `innerHTML` (unlike `textContent`) can open the door for XSS (Cross-Site Scripting) attacks on your application. If the content being inserted into the DOM via `innerHTML` is not completely trusted, an attacker could use a ` – ChaseMoskal Mar 17 '16 at 04:12
  • The performance statement in the citation of MDN "in order to retrieve or write text within an element" is a bit misleading, because the argument of having to be "parsed as HTML" is true only for the setter, but not for the getter. – Sebastian Mar 24 '23 at 18:32
17

Addendum to JLRishe's otherwise excellent answer:

The reason innerText and outerText both exist is for symmetry with innerHTML and outerHTML. This becomes important when you assign to the property.

Suppose you've got an element e with HTML code <b>Lorem Ipsum</b>:

e.innerHTML = "<i>Hello</i> World!"; => <b><i>Hello</i> World!</b>
e.outerHTML = "<i>Hello</i> World!"; =>    <i>Hello</i> World!
e.innerText = "Hello World!";        => <b>Hello World!</b>
e.outerText = "Hello World!";        =>    Hello World!
Anonymous
  • 179
  • 1
  • 2
9

A dropdown list comprises a collection of Option objects, so you should use the .text property to inspect the textual representation of the element, i.e.

<option value="123">text goes here</option>
                    ^^^^^^^^^^^^^^

Btw,

.text appears to be the same as .innerText but the JQuery shorthand version

That's not correct; $(element).text() is the jQuery version whereas element.text is the property access version.

Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
8

text and label remove extra spaces. I got these results when querying options in a dropdown:

e.textContent = "A    B C   D     "
e.text = "A B C D"
e.label = "A B C D"
Jay Jay
  • 117
  • 1
  • 4
  • upvoted because everyone talked the others but you were the only one to compare `label` and `text` – MrU Oct 25 '20 at 08:22
  • @Lars-Lasse Yes, they are part of the DOM API; they’re getters / setters on [`HTMLOptionElement`s](//developer.mozilla.org/docs/Web/API/HTMLOptionElement#properties), as the answer suggests. See [Web IDL definitions in the HTML spec](//html.spec.whatwg.org/multipage/form-elements.html#htmloptionelement). This answer is potentially misleading and incomplete. – Sebastian Simon Dec 16 '21 at 17:12
  • @SebastianSimon Thanks mate, didn't notice it was regarding the – Lars-Lasse Dec 17 '21 at 20:57
4

textContent will not format (\n)

Sagar V
  • 12,158
  • 7
  • 41
  • 68
Hariharan
  • 79
  • 3
1

See the browsers compatibility http://www.quirksmode.org/dom/html/ if you are targeting specific browsers. Because it seems like they all have their own way of doing things. That is why is is better to use JQuery .text() (http://api.jquery.com/text/) if you do not want to fiddle around.

nywooz
  • 351
  • 2
  • 8