-
June 1st, 2009
Hatin’ on textContent and innerText
It’s surprisingly annoying to get just the text content of an html element in a cross-browser friendly way. I wrote
Element#getTextContent()to take care of this simple operation for me.The Prototype Version:
Element.addMethods({ /** * Element#getTextContent(@element) -> String * Cross-browser means of getting Element#textContent or Element#innerText **/ getTextContent: function(element) { if (!Object.isUndefined(element.textContent)) { return element.textContent; } return element.innerText; } });The Procedural Version
/** * Gets the text content of the specified element. * @param element {HTMLElement} The html element * @return {String} The string content of the specified element. */ function getTextContent(element) { if (typeof element.textContent != "undefined") { return element.textContent; } return element.innerText; }