123
123

Reputation: 525

How to get textContent to work in Internet Explorer 8 (JavaScript)

I have applied a textContent to a div in JavaScript. I cannot get it to work in Internet Explorer. How can I get it to work in Internet Explorer 8. I tried doing :

<div id="price"></div>

and:

document.getElementById("price").textContent = "GMGo is $5.00";

but it didn't show the text "GMGo is $5.00"

Upvotes: 1

Views: 5766

Answers (1)

SheetJS
SheetJS

Reputation: 22925

ie8 does not support textContent, but there is a way to fake it:

http://eligrey.com/blog/post/textcontent-in-ie8

if (Object.defineProperty && Object.getOwnPropertyDescriptor &&
     Object.getOwnPropertyDescriptor(Element.prototype, "textContent") &&
    !Object.getOwnPropertyDescriptor(Element.prototype, "textContent").get)
  (function() {
    var innerText = Object.getOwnPropertyDescriptor(Element.prototype, "innerText");
    Object.defineProperty(Element.prototype, "textContent",
      { // It won't work if you just drop in innerText.get
        // and innerText.set or the whole descriptor.
        get : function() {
          return innerText.get.call(this)
        },
        set : function(x) {
          return innerText.set.call(this, x)
        }
      }
    );
  })();

Upvotes: 8

Related Questions