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.