innerHTML without the html, just text

cphilpot picture cphilpot · Mar 15, 2012 · Viewed 43.9k times · Source

I've created an e-mail link that automatically populates the necessary information in the body. But, when I do .innerHTML I get a little more than I bargained for.

I want "March, 2012: 12-16"

What I get <B>March, 2012</B>: <FONT color=blue>12</FONT> - <FONT color=blue>16</FONT>

Is there a way to get the innerHTML without the html tags?

.value = undefined
.text = undefined

Answer

apsillers picture apsillers · Mar 15, 2012

You want .textContent in all but older IE, and .innerText in IE (<9).

So, try:

string = (node.textContent===undefined) ? node.innerText : node.textContent;

EDIT: Or, just use GGG's much cleaner string = (node.innerText || node.textContent), since undefined is falsy.