MyDaftQuestions
MyDaftQuestions

Reputation: 4701

innerText vs innerHTML vs label vs text vs textContent vs outerText

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:

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:

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?

Upvotes: 176

Views: 106647

Answers (7)

Alex from Jitbit
Alex from Jitbit

Reputation: 60832

Notable additions:

  1. .innerText is the only one that respects CSS and preserves line breaks, essentially "rendering" block elements like BR or DIV as new lines. No answers mentioned that so I decided to add this point.

  2. Both .textContent and text() will not do that

  3. Additionally .text() and label will also collapse white spaces as other answers suggested.

Upvotes: 0

Jay Jay
Jay Jay

Reputation: 147

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"

Upvotes: 11

JLRishe
JLRishe

Reputation: 101748

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.

Upvotes: 145

Hariharan
Hariharan

Reputation: 79

textContent will not format (\n)

Upvotes: 4

Anonymous
Anonymous

Reputation: 179

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!

Upvotes: 17

nywooz
nywooz

Reputation: 349

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.

Upvotes: 1

Ja͢ck
Ja͢ck

Reputation: 173652

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.

Upvotes: 9

Related Questions