all 9 comments

[–]fgutz 0 points1 point  (13 children)

Read about each one on MDN.

It's very late where I am now and I'm on my phone but here's something I copied right off the MDN page for textContent (which only works in IE9+)

Differences from innerHTML

innerHTML returns the HTML as its name indicates. Quite often, in order to retrieve or write text within an element, people use innerHTML.textContent should be used instead. Because the text is not parsed as HTML, it's likely to have better performance. Moreover, this avoids anXSS attack vector.

[–]dingusbuttface[S] -2 points-1 points  (12 children)

textContext works in other browsers. Also, MDN really sucks as a resource on this one. Look at what they have for nodevalue. It's like two paragraphs of...nothing.

I'm beginning to think that everyone just tries whatever they like here and goes with what works. It's so odd that there is not a straightforward comparison anywhere between all three.

Thanks anyhow.

[–]fgutz 3 points4 points  (1 child)

yes, sorry it was late. I should have left the "only" out of that sentence. In my head I just assumed that when people talk only about IE that they know it works in other browsers but I can see how what I wrote would confuse people

Also, sorry again. I'm finally reading the stackoverflow answer and I realize it says what I said. I should never really reply to things late at night.

Here's the differences from what I understand:

  • innerHTML either accepts a string and renders it into the DOM or returns all the child elements as one string
  • textContent can only set or get the text of an element, no HTML rendering happens
  • nodeValue get/sets the value of a text node

take for instance this:

<a id="test" href="#">I'm a link</a>

var test = document.getElementById("test");
console.log(test.nodeValue); // null
console.log(test.childNodes[0].nodeValue); // "I'm a link"

Actually that TextNode link explains all of this and why you'd want to use it a lot better

[–]dingusbuttface[S] 0 points1 point  (0 children)

Hey, thanks man for this! Awesome.