Ron Brennan wrote:
I want if possible to use something like:
element.getElem entsByTagName(' text')
but that doesn't work. Is there another value for the parameter, or is it
not possible with getElementsByTa gName?
As Mick said you cannot do it with getElementsByTa gName, 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.getEle mentById &&
document.create TreeWalker &&
typeof NodeFilter!="un defined"
){
var tw=document.cre ateTreeWalker(
document.getEle mentById("foo") ,
NodeFilter.SHOW _TEXT,
null,
false
);
var node;
var buf=[];
while(node=tw.n extNode())
buf.push(node.n odeValue);
alert(buf.lengt h ? buf.join("\n") : "No text node iterated");
}
</script>
---
HTH,
Yep.