ja*********@gmail.com wrote:
I cannot get this figured out. I need to get the text out of an
element node. In IE, I can do it like this:
xNode(0).text
but that does not work for Mozilla. And since element nodes return
NULL for nodeValue, what am I supposed to do?
Within the W3C DOM Level 1 and 2 there is no property or function that
simply gives you the text content of an element node so there you would
need to walk the subtree under the element and concatenate the content
of any text nodes you find.
Within the W3C DOM Level 3 a property named textContent is defined so
there you can do (with ECMAScript/JavaScript)
element.textContent
Although Mozilla has no complete support for Level 3 the property
textContent is implemented in newer Mozilla versions so depending on
which Mozilla versions you target you can use that property or need to
walk the subtree:
function getInnerText (node) {
if (typeof node.textContent != 'undefined') {
return node.textContent;
}
else if (typeof node.innerText != 'undefined') {
return node.innerText;
}
else if (typeof node.text != 'undefined') {
return node.text;
}
else {
switch (node.nodeType) {
case 3:
case 4:
return node.nodeValue;
break;
case 1:
case 11:
var innerText = '';
for (var i = 0; i < node.childNodes.length; i++) {
innerText += getInnerText(node.childNodes[i]);
}
return innerText;
break;
default:
return '';
}
}
}
--
Martin Honnen
http://JavaScript.FAQTs.com/