innerText vs outerText
Asked Answered
S

2

37

After searching through web i understood the difference between innerHTML and outerHTML.

However i am having hard time understanding the difference between innerText and outerText. Both appear almost same to me.

Can anyone help me understand this with a nice illustration ?

Thank You !

Sweepstakes answered 28/8, 2013 at 7:5 Comment(1)
First Google result described it pretty well: outerText: Enables you to change all the element’s text, including the start and end tags.Radmen
J
84

innerText changes only text within HTML tags, e.g.

<div>
  <p>Change Me</p>
</div>

p.innerText = "Changed!"

Becomes

<div>
  <p>Changed!</p>
</div>

Whereas outerText:

<div>
  <p>Change Me</p>
</div>

p.outerText = "Changed!"

Becomes

<div>
   Changed!
</div>
Jerz answered 28/8, 2013 at 7:10 Comment(1)
Excellent answer, short and sweet.Greasepaint
S
19

Basically,
innerText: what's between the tags of the element.
outerText: content of the element, including the tags.

Sociology answered 28/8, 2013 at 7:10 Comment(1)
Just as a side note: developer.mozilla.org/en-US/docs/Web/API/HTMLElement/outerText This feature is non-standard and is not on a standards track. Do not use it on production sites facing the Web: it will not work for every user. There may also be large incompatibilities between implementations and the behavior may change in the future.Kappa

© 2022 - 2024 — McMap. All rights reserved.