views:

49

answers:

3

I have written a lot of pages last month and today I realized that innerText doesn't work in Firefox. I found textContent, but I don't want to dig my tons of pages. Also sometimes I used jQuery.text, it seems to work everywhere.

So question: what is the simplest way out of this hell?

+1  A: 

Search and replace innerText with the .text() function.

Darin Dimitrov
ye. but its also not so easy
eba
As you said there are not always easy ways out of the hell :-) A decent editor and appropriate regular expression could make this a single click task.
Darin Dimitrov
ok. thx. i think i have no choice)
eba
A: 

I would never use this in production code but...

if ( typeof HTMLElement != 'undefined' && HTMLElement.prototype.__defineGetter__ != 'undefined' ) {
    HTMLElement.prototype.__defineGetter__("innerText", function () {
        return this.textContent;
    });
}
epascarello
A: 

What about using the DOM approach?

var elem = document.getElementById("ElementID");
var text = document.createTextNode("Text");
text.nodeValue += " and more Text";
elem.appendChild(text);

It's not so search-and-replace friendly, but it will guarantee you compatibility across the browsers.

You also may need to remove all the elements from the node if you use this approach. You can do that like:

while ( elem.hasChildNodes() ) {
    elem.removeChild( elem.firstChild );
}
palswim