Javascript tip: innerText or textContent

Just a quick tip how to handle using an UI object’s innerText property properly. Even today there are differences on how web browsers give you access to the ‘Text’ content of an HTML element. Let say you have the following html:

<p id=”mine”>This is some text</p>

To get the ‘content you must use the following:

IE and Chrome

mine.innerText

but in Firefox

mine.textContent

To make a ‘universal function to get this text you can use the following function:

function getInnerText(obj){
  if (obj){
    if (obj.innerText)
      return obj.innerText;
    else
      return obj.textContent
    }
  else
    return "";
}

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>