...

View Full Version : add a getChildrenByTagName function



pbru
12-19-2004, 08:50 PM
hi,

if I write a helper function getChildrenByTagName (the same as getElementsByTagName but whicj search only the direct children of a node) and if I write:

document.getChildrenByTagName = function(tagName) {...}

will this function be available for every node name or only for document ? if only for document, how can make it available for every node ?

TIA,
Pierre.

hemebond
12-19-2004, 09:04 PM
I think it's something like
HTMLElement.prototype.getChildrenByTagName = function...Do a Google for that.

pbru
12-20-2004, 03:14 PM
thanx.

Pierre.

jbot
12-22-2004, 11:46 AM
HTMLElement.prototype.getChildrenByTagName = function...

that wouldn't work in IE tho, since it doesn't support HTMLElement. :(

jbot
12-22-2004, 12:00 PM
give this a try:


document.getChildrenByTagName=function(tagName)
{
for (var i=0, aTags=document.getElementsByTagName(tagName), aChildren=[]; i<aTags.length; i++)
{
if (aTags[i].nodeType==1 && aTags[i].childNodes>0)
{
for (var j=0, aNodes=aTags[i].childNodes; j<aNodes.length; j++) aChildren.push(aNodes[j]) ;
}
}

// aChildren is a vector (an array of objects) containing all the childnodes of each tag as an individual entry
return aChildren;
}

pbru
12-23-2004, 04:35 PM
jbot: I did not thought of this interpretation of getChildrenByTagName :D

my idea was to get all the first level children of a node which have the tag tagName, not all the descendants of that node that have the tag tagName as wouls do getElementsByTagName. a kind of non-recursive getElementsByTagName

meanwhile, I found that XPath couls do that for me.

Pierre.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum