Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Dec 2004
    Location
    Toulouse, France
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    add a getChildrenByTagName function

    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.

  • #2
    Senior Coder
    Join Date
    Jul 2004
    Location
    New Zealand
    Posts
    1,315
    Thanks
    0
    Thanked 2 Times in 2 Posts
    I think it's something like
    Code:
    HTMLElement.prototype.getChildrenByTagName = function...
    Do a Google for that.

  • #3
    New to the CF scene
    Join Date
    Dec 2004
    Location
    Toulouse, France
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanx.

    Pierre.

  • #4
    Senior Coder
    Join Date
    Feb 2004
    Location
    Edinburgh
    Posts
    1,352
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by hemebond
    Code:
    HTMLElement.prototype.getChildrenByTagName = function...
    that wouldn't work in IE tho, since it doesn't support HTMLElement.
    *keep it simple (TM)

  • #5
    Senior Coder
    Join Date
    Feb 2004
    Location
    Edinburgh
    Posts
    1,352
    Thanks
    0
    Thanked 0 Times in 0 Posts
    give this a try:

    PHP Code:
    document.getChildrenByTagName=function(tagName)
    {
        for (var 
    i=0aTags=document.getElementsByTagName(tagName), aChildren=[]; i<aTags.lengthi++)
        {
            if (
    aTags[i].nodeType==&& aTags[i].childNodes>0
            {
                for (var 
    j=0aNodes=aTags[i].childNodesj<aNodes.lengthj++) 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;

    *keep it simple (TM)

  • #6
    New to the CF scene
    Join Date
    Dec 2004
    Location
    Toulouse, France
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    jbot: I did not thought of this interpretation of getChildrenByTagName

    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •