By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,636 Members | 1,213 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,636 IT Pros & Developers. It's quick & easy.

How do I get the value of a text node?

P: n/a
Hi,

If I have a TD, whose id = "myTd," that only contains text within
it,how do extract that text?

Thanks, - Dave
Jul 21 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
laredotornado <la***********@zipmail.comwrites:
If I have a TD, whose id = "myTd," that only contains text within
it,how do extract that text?
Find all child nodes that are text nodes, and extract their content.
Even if the td only contains text, that text might be split over
more than one text node (worst case).

var td = document.getElementById("myTd");
var textNodeContents = [];
for(var chld = td.firstChild; chld; chld = chld.nextSibling) {
if (chld.nodeType == 3) { // text node
textNodeContents.push(chld.nodeValue);
}
}
var text = textNodeContents.join("");
/L
--
Lasse Reichstein Nielsen
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 21 '08 #2

P: n/a
On Jul 21, 2:19*pm, Lasse Reichstein Nielsen <l...@hotpop.comwrote:
laredotornado<laredotorn...@zipmail.comwrites:
If I have a TD, whose id = "myTd," that only contains text within
it,how do extract that text?

Find all child nodes that are text nodes, and extract their content.
Even if the td only contains text, that text might be split over
more than one text node (worst case).

var td = document.getElementById("myTd");
var textNodeContents = [];
for(var chld = td.firstChild; chld; chld = chld.nextSibling) {
* if (chld.nodeType == 3) { // text node
* * textNodeContents.push(chld.nodeValue);
* }}

var text = textNodeContents.join("");

/L
--
Lasse Reichstein Nielsen
*DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
* 'Faith without judgement merely degrades the spirit divine.'
I'm noticing some odd behavior. When I call

var ele = document.getElementById(id);
var innerHtml = ele.innerHTML;
var text = ele.nodeValue;

The value of "innerHtml" yields a value whereas the value of "text"
comes up empty. The object type of ele is [object
HTMLTableCellElement].

Any other insights are greatly appreciatd, - Dave
Jul 21 '08 #3

P: n/a
laredotornado <la***********@zipmail.comwrites:
I'm noticing some odd behavior. When I call

var ele = document.getElementById(id);
var innerHtml = ele.innerHTML;
var text = ele.nodeValue;

The value of "innerHtml" yields a value whereas the value of "text"
comes up empty. The object type of ele is [object
HTMLTableCellElement].
That's because table cells, and in fact most HTML node types, do not
have a useful nodeValue property. HTML text is part of the value of
Text nodes, which are children of the cell (if there is any text).

IOW, any characters "in between" tags and some other special
constructs results in one or more Text nodes containing the character
data.
Any other insights are greatly appreciatd, - Dave
See http://developer.mozilla.org/en/docs...ment.nodeValue

--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
Jul 21 '08 #4

P: n/a
Joost Diepenmaat <jo***@zeekat.nlwrites:
IOW, any characters "in between" tags and some other special
^
not part of
constructs results in one or more Text nodes containing the character
data.
--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
Jul 21 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.