469,935 Members | 1,632 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,935 developers. It's quick & easy.

element text

Hi,

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? is there a way to
convert an element node into a text node?

Thanks,

Jason

Sep 22 '05 #1
1 1562


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/
Sep 23 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Stefan Franke | last post: by
4 posts views Thread by Rithish | last post: by
6 posts views Thread by Luke Dalessandro | last post: by
7 posts views Thread by David | last post: by
2 posts views Thread by yawnmoth | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.