473,388 Members | 1,198 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

How do I get the value of a text node?

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
4 1936
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Tim:.. | last post by:
Hi Possibly a very simple question but how do I get a value out of an XML document so I can play with it in ASP E.G: <Name>Tom</Name How do I pull the work tom into asp Thanks
3
by: Phoenix | last post by:
I am trying to print date headings over comments (as headings) I have a simple XML file : <comments> <comment id=1234 yyyymmdd="20041230" flag="Y">..text..</comment> <comment id=1309...
12
by: Anna | last post by:
Hi all, I posted the same question this afternoon but my message isn't showing up, so I thought I'd give it another try.... in case you should see it later I apologize for posting the same...
21
by: Michael Bierman | last post by:
Please forgive the simplicy of this question. I have the following code which attempts to determine the color of some text and set other text to match that color. It works fine in Firefox, but does...
3
by: keepyourstupidspam | last post by:
Hi, I am using xerces dom C++, I want to change an element value, here is the code I am using but the element in the file is not getting updated. ... ... ... xercesc_2_4::DOMDocument*...
2
by: Greg | last post by:
Hi. I have a rather large xml document (object) that can have one or more nodes with a certain attribute throughout (at ANY depth, not at the same level necessarily). I need to find this...
1
by: Dica | last post by:
hi all first off, i'm not trying to cross post, but couldn't find this newsgroup earlier (got here from a recommendation on microsoft.public.vb, where i originally posted this question). ...
0
by: XML newbie: Urgent pls help! | last post by:
ok, I changed "If node.Text = "Success" " to "If Not loginDom.InnerText Is Nothing ". Now, I don't get the NullReference error but the SessionID I see on screen is concatanated one. It looks...
3
by: Goran Djuranovic | last post by:
Hi All, Does anyone know how to retreive deepest XPath value from XML document by using VB.NET? For example, if I had an XML file like this: <Root> <Customer> <Name>MyName</Name> </Customer>...
2
by: esebastian | last post by:
Hi all, Basically i want to do get the specific node that is empty but when i try the following i get a null node even though I know there is a node whose AN value is null. What am i doing wrong?...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.