Monday, June 2, 2008

JavaScript textContent vs innerText

OK JavaScript fans, this week's tip isn't so much a tip as a nod and link to the guys over at CoderLab. 

I was having problems last week with innerText -- the property of DOM nodes that capture the text without the HTML tags. And I was having this problem with Firefox and no other browsers. And that just didn't make sense to me. Sure, if it has been IE I'd have called it par for the course, but I just wasn't expecting it from Firefox.

Anywho, as it turns out, Firefox 1.5 uses the textContent property instead of innerText even though the DOM standard says innerText. Great move guys.

At any rate CoderLab wrote about this one back in April of 06, but I only just found a need for it this past week as I was finishing SociaLink. Here's the link to CoderLab:


4 comments:

Anonymous said...

textContent is the standard. IE, as usual, is wrong.

Anonymous said...

http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/core.html#Node3-textContent

Unknown said...

textContent doesn't appear to work in FireFox 3.5.9, but stupidly enough, .value DOES return the contents of a "textarea" tag. Anyone know why?

Anonymous said...

As the first commenter said, IE did get it wrong, no surprise there.

@Jim
input fields use `value`. The value is what is posted back to the server. `textContent` is just for display and isn't included in a post response. So `textContent` will work in pretty much everything but IE for div, span, p etc.