473,396 Members | 1,827 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,396 software developers and data experts.

How do I get an element's the text node

I want if possible to use something like:

element.getElementsByTagName('text')

but that doesn't work. Is there another value for the parameter, or is it
not possible with getElementsByTagName?

Thanks,
Ron

Jul 23 '05 #1
6 28368
Ron Brennan wrote:
I want if possible to use something like:

element.getElementsByTagName('text')

but that doesn't work. Is there another value for the parameter, or is it
not possible with getElementsByTagName?


As far as I know, no, unless you have a tag named <text>, in which case
"element.getElementsByTagName('text')" returns a collection of <text>
elements contained in "element"

Mick

Jul 23 '05 #2
Ron Brennan wrote:
I want if possible to use something like:

element.getElementsByTagName('text')

but that doesn't work. Is there another value for the parameter, or is it
not possible with getElementsByTagName?


As Mick said you cannot do it with getElementsByTagName, this method
returns *elements*, i.e. nodes of type "element", and not other types of
nodes (at least it shouldn't).

If you want to retrieve only text nodes, then you'll have to build
custom node iterators; depending on your requirements this may not be
too difficult.

FYI the W3C has defined traversal objects, namely Treewalkers or
NodeIterators, which permit to traverse the tree in a certain order,
with node filters, and much more. Unfortunately these are not supported
in IE (and probably lots of other browsers), which makes the objects
unusable for the web.

<URL:http://www.w3.org/TR/2000/REC-DOM-Level-2-Traversal-Range-20001113/traversal.html>

Try the following in Opera 8 or Mozilla 1.7+ to get an illustration.

---
<div id="foo">Hello,<span>World</span>!</div>

<script type="text/javascript">
if(
document.getElementById &&
document.createTreeWalker &&
typeof NodeFilter!="undefined"
){

var tw=document.createTreeWalker(
document.getElementById("foo"),
NodeFilter.SHOW_TEXT,
null,
false
);
var node;
var buf=[];

while(node=tw.nextNode())
buf.push(node.nodeValue);

alert(buf.length ? buf.join("\n") : "No text node iterated");
}
</script>
---
HTH,
Yep.
Jul 23 '05 #3
Yup, it appears impossible that way. The following is an acceptable
solution:

var text = getElementById("tdElement").firstChild.data;

Jul 23 '05 #4
VK
> The following is an acceptable solution:
var text = getElementById("tdElement").fi*rstChild.data;


If you're working with your own pages where you're the king.

Universally *in HTML* there is no guarantee that the firstChild will
always point to the text node (even if you see nothing but text in the
element of question). It may return an empty text node created say on
place of the page break.

Jul 23 '05 #5

If you're looking for the text of the node with no html and all
concatenated in one, so called NORMALIZED()'ed node, for mozilla
obj.textContent gives you all that, stripped of all html and any null
chars like tabs and CR's, for IE/opera you can use obj.innerText, is
exactly the same object. Now, if you want to do it per node over the
childNodes collection, then you can iterate of the object you want to
check, check if its obj.childNodes.length is greater than 0, or has
children, then check each of its children for obj.childNodes.length as
well, for any children in them. Anyhow, in the loop, check each
obj.nodeType, #text nodes have a DOM nodeType of 3, elements are 1,
attributes are 2 and so on, check your DOM glossary if you wish.
Danny

On Sat, 02 Jul 2005 10:55:08 -0700, Ron Brennan <rb******@magma.ca> wrote:
--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
Jul 23 '05 #6
VK wrote:
The following is an acceptable solution:
var text = getElementById("tdElement").fi*rstChild.data;
If you're working with your own pages where you're the king.


No, because it has to be `document.getElementById'.
Universally *in HTML* there is no guarantee that the firstChild will
always point to the text node (even if you see nothing but text in the
element of question).
Correct, the text may be marked up by another element.
It may return an empty text node created
There is no such thing as an empty text node in an unchanged DOM tree;
either the text node contains whitespace or the (first child node) node
is an element node.
say on place of the page break.


What is a page break in HTML?
PointedEars
Jul 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Juliano Freitas | last post by:
How can i get the text between the <teste> tags?? >>> xml = """<root><teste> texto </teste></root>""" >>> from xml.dom import minidom >>> document = minidom.parseString(xml) >>> document...
2
by: Martin | last post by:
Hallo, can you help me writing a generic xslt transformation (useable with xsql from oracle)? The problem is how to get the escaping characters .... === INPUT-File in.xml <?xml version =...
5
by: Patient Guy | last post by:
In my reading of the Strict and Transitional DTD for HTML 4.0, the table row (TR) elements are contained within table section elements: THEAD, TFOOT, and TBODY. The table section elements are...
6
by: Luke Dalessandro | last post by:
I'm not sure if this is the correct forum for platform specific (Mozilla/Firefox) javascript problems, so just shout and point me to the correct newsgroup if I'm being bad. Here's the deal... ...
3
by: Robert Oschler | last post by:
What's a good way to find a specific text node element in a web page's DOM tree? I thought of traversing each node but there has to be a faster way. Is there a "find text node by nodeValue"...
1
by: jason.lucey | last post by:
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...
8
by: bennett.matthew | last post by:
Hello all, This is probably an elementary (no pun intended) question, but I've spent all afternoon on it and it's driving me crazy. I have a function which dynamically adds to a table. It...
3
markmcgookin
by: markmcgookin | last post by:
Hi, I have the following XML <AnswerList xmlns="http://tempuri.org/ALPS_Assessmentv1p1_RESCO_Schema.xsd"> <DateTimeLastSaved>12:12:12 1900</DateTimeLastSaved> <UserName>Bob</UserName>...
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: 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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.