The poor, misunderstood innerText (2015)
perfectionkills.com
perfectionkills.com
I had to look this ( innerText vs textContent) up yesterday and MDN had a much clearer explanation.
"The innerText property of the HTMLElement interface represents the rendered text content of a node and its descendants."
https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement...
HTML:
<div id="example">
<p style="display: none;">Hidden text</p>
<p>Visible text</p>
</div>
Javascript: const exampleDiv = document.querySelector('#example');
console.log(exampleDiv.innerText); // "Visible text"
console.log(exampleDiv.textContent); // "Hidden textVisible text"- innerText does what a human would likely want (fuzzy)
- textContent does what's technically correct (strict)
or to put it another way:
- retrieving textContent will give you what an author writing HTML by hand likely intended
- retrieving innerText will give you what an author writing into a WYSIWYG likely intended
The problem with fuzzy things like this is their implementations are much more likely to be buggier, but - barring bugs & inconsistencies (which should be ironed out by now with the HTML5 spec) - the intent of innerText seems preferable for most real-world "getter" use-cases.
For "setters" textContent does seem better, but I find in practice I use element.appendChild(document.createTextNode(value)) more frequently since it's non-destructive.
Back then when those things were new, there was absolutely no way to store a string in an element and get it back later. Neither of those do this.
Your mental model is a little off here. The character data returned will be coming from an internal representation of a node hierarchy - there are no tags, just a collection of nodes. The text data will be assembled from all text nodes in that collection and concatenated. Nothing is being stripped.
I'd guess the difference in original pre-spec features was innerText taking an internal representation that had been pre-processed for rendering before being stored in memory.
> innerText strictly preserves the HTML semantics of your string, textContent doesn't preserve anything.
It's the opposite. textContent preserves the semantics of your string in HTML source. innerText returns an intermediate representation of how you would expect your string to be handled by the rendering pipeline.
> Back then when those things were new, there was absolutely no way to store a string in an element and get it back later. Neither of those do this.
Not sure what you mean by this?
The Poor, Misunderstood InnerText (2015) - https://news.ycombinator.com/item?id=25176309 - Nov 2020 (13 comments)
The poor, misunderstood innerText - https://news.ycombinator.com/item?id=10167066 - Sept 2015 (1 comment)
The poor, misunderstood innerText - https://news.ycombinator.com/item?id=9304606 - April 2015 (32 comments)
EDIT: Weird. After returning here and clicking the link again, it now renders fine... it doesn't even show the warning, and now has a lock icon in the URL bar?
(Also, you gotta love the fact that a site called "perfection kills" doesn't implement HTTPS - fair play to the author.)
Ugh, and here I was thinking this one was the 'better' one to use than innerHTML (if you can get away with it, when needing just unformatted text and not needing the convenience of HTML tags being automatically formatted for you)
See MDN's docs on innerText. It's fully supported in Firefox since version 45 (2016). (Edit: the blog post also has an update at the bottom, noting that, as of 2016, the situation was already much better.)
https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement...
It's always been considered better than innerHTML though; it's only textContent people think it's worse than.
I think this post basically spec'd it, did the competitive analysis and outlined a clear why tho!
How do these standards processes work? It seems like there's demand.
There was, and it has since been standardised.
https://html.spec.whatwg.org/multipage/dom.html#the-innertex...
Chrome implements something, and 3 years later it's adopted as a standard because the other browser (Firefox) was forced to implement it anyway.
There could be other uses.