element text

J

jason.lucey

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
 
M

Martin Honnen

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);
}
return innerText;
break;
default:
return '';
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,000
Messages
2,570,252
Members
46,848
Latest member
CristineKo

Latest Threads

Top