‘innerText’ works in IE, but not in Firefox

‘innerText’ works in IE, but not in Firefox

I have some JavaScript code that works in IE containing the following:
myElement.innerText = “foo”;

However, it seems that the ‘innerText’ property does not work in Firefox. Is there some Firefox equivalent? Or is there a more generic, cross browser property that can be used?

Solutions/Answers:

Solution 1:

Firefox uses the W3C-compliant textContent property.

I’d guess Safari and Opera also support this property.

Solution 2:

Update: I wrote a blog post detailing all the differences much better.


Firefox uses W3C standard Node::textContent, but its behavior differs “slightly” from that of MSHTML’s proprietary innerText (copied by Opera as well, some time ago, among dozens of other MSHTML features).

First of all, textContent whitespace representation is different from innerText one. Second, and more importantly, textContent includes all of SCRIPT tag contents, whereas innerText doesn’t.

Just to make things more entertaining, Opera – besides implementing standard textContent – decided to also add MSHTML’s innerText but changed it to act as textContent – i.e. including SCRIPT contents (in fact, textContent and innerText in Opera seem to produce identical results, probably being just aliased to each other).

textContent is part of Node interface, whereas innerText is part of HTMLElement. This, for example, means that you can “retrieve” textContent but not innerText from text nodes:

var el = document.createElement('p');
var textNode = document.createTextNode('x');

el.textContent; // ""
el.innerText; // ""

textNode.textContent; // "x"
textNode.innerText; // undefined

Finally, Safari 2.x also has buggy innerText implementation. In Safari, innerText functions properly only if an element is
neither hidden (via style.display == "none") nor orphaned from the document. Otherwise, innerText results in an empty string.

I was playing with textContent abstraction (to work around these deficiencies), but it turned out to be rather complex.

You best bet is to first define your exact requirements and follow from there. It is often possible to simply strip tags off of innerHTML of an element, rather than deal with all of the possible textContent/innerText deviations.

Another possibility, of course, is to walk the DOM tree and collect text nodes recursively.

Solution 3:

If you only need to set text content and not retrieve, here’s a trivial DOM version you can use on any browser; it doesn’t require either the IE innerText extension or the DOM Level 3 Core textContent property.

function setTextContent(element, text) {
    while (element.firstChild!==null)
        element.removeChild(element.firstChild); // remove all existing content
    element.appendChild(document.createTextNode(text));
}

Solution 4:

jQuery provides a .text() method that can be used in any browser. For example:

$('#myElement').text("Foo");

Solution 5:

As per Prakash K’s answer Firefox does not support the innerText property. So you can simply test whether the user agent supports this property and proceed accordingly as below:

function changeText(elem, changeVal) {
    if (typeof elem.textContent !== "undefined") {
        elem.textContent = changeVal;
    } else {
        elem.innerText = changeVal;
    }
}

Solution 6:

A really simple line of Javascript can get the “non-taggy” text in all main browsers…

var myElement = document.getElementById('anyElementId');
var myText = (myElement.innerText || myElement.textContent);