27

According to the HTML5 specification and the DOM specification an HTMLAnchorElement has a text and a textContent property. What is the purpose of the text property? As far as I can tell text is just a read-only getter for textContent

Peter Olson
  • 139,199
  • 49
  • 202
  • 242
fynyky
  • 493
  • 2
  • 6
  • 14

2 Answers2

16

The textContent property is "inhertied" from the Node interface of the DOM Core specification. The text property is "inherited" from the HTML5 HTMLAnchorElement interface and is specified as "must return the same value as the textContent IDL attribute".

The two are probably retained to converge different browser behaviour, the text property for script elements is defined slightly differently.

Note that the DOM specification is a general specification for any kind of document (e.g. HTML, XML, SGML, etc.) whereas HTML5 is specifically for HTML that leverages and extends the DOM Core in many respects (some might say it's a "super set" of a few DOM specs plus HTML plus …).

Note that "inherited" does not mean "prototype inheritance", just the more general meaning of inherited.

RobG
  • 142,382
  • 31
  • 172
  • 209
  • 2
    Is there any purpose to `text` then? Since HTML is an extension of the DOM Core I can't imagine any scenario which you could use `text` over `textContent` – fynyky Nov 01 '12 at 07:07
  • 2
    In DOM 2 HTML, only 4 elements had a text property and it did different things (e.g. for [body](http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-73714763) it was the document text colour, for [title](http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-77500413) it was the title text, also [option](http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-48154426) and [script](http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-46872999)). But support was inconsistent, HTML5 seems to be trying to converge text with textContent. It is as much about "what browsers actually do" as "what they should do". – RobG Nov 01 '12 at 12:58
  • 2
    if `text` and `textContent` are the same then I'd use `text` as it's shorter. – gman Mar 23 '16 at 21:42
  • I am using the XML DOM in IE11, and it seems to only have a textContent property, and no text property. So to be consistent between the XML DOM and HTML DOM, it seems to make sense to always use textContent. – Polyfun Aug 04 '16 at 09:06
  • In a list with an option like `foobar`, that list.options[i].tex returns `foobar` and list.options[i].textContent returned `foobar`. Unfortunantely it looks like .text just not giving me a string with multiple whitespaces. => stands for a whitespace – Butti Apr 08 '20 at 20:56
2

I recommend using textContent as seen on MDN and W3C quirksmode.org

The text property you referenced from DOM Standard, refers to the Text Web API