What's the the difference between innerText
, text()
, and html()
?
The innerText property of the HTMLElement interface represents the rendered text content of a node and its descendants. As a getter, it approximates the text the user would get if they highlighted the contents of the element with the cursor and then copied it to the clipboard.
In addition to innerText not working in Firefox: textContent seems to work in all major browsers, so just use textContent instead of innerText.
It then adds them to the DOM separately in a manner that causes their execution. .html() implicitly causes a few operations (script handling being one) whereas writing to innerHTML simply causes the innerHTML to change, but very little is done with that HTML.
innerText
(or text()
if you're using jQuery) doesn't include any HTML tags. So if you had a div that contained:
View my <a href="profile.html">profile</a>
innerText
/ text()
would return
View my profile
while html()
would return
View my <a href="profile.html">profile</a>
As dcneiner points out html()
/text()
are jQuery properties (and supported across browsers) while innerText is not implemented by all browsers (although it works in the most recent versions of IE, Safari, and Chrome).
Basically you'll want to use text()
isntead of innerText whenever possible. See dcneiner's post (or the jQuery docs) for some other things that make text()
awesome.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With