Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between innerText and html

What's the the difference between innerText, text(), and html()?

like image 741
BeraCim Avatar asked Dec 03 '09 00:12

BeraCim


People also ask

What is innerText?

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.

What can I use instead of innerText?

In addition to innerText not working in Firefox: textContent seems to work in all major browsers, so just use textContent instead of innerText.

What is the difference between HTML and innerHTML?

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.


1 Answers

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.

like image 146
Chris Van Opstal Avatar answered Oct 20 '22 01:10

Chris Van Opstal