469,580 Members | 1,888 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

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 27701
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Juliano Freitas | last post: by
6 posts views Thread by Luke Dalessandro | last post: by
3 posts views Thread by Robert Oschler | last post: by
1 post views Thread by jason.lucey | last post: by
8 posts views Thread by bennett.matthew | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.