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 5 of 5
  1. #1
    ubh
    ubh is offline
    Regular Coder ubh's Avatar
    Join Date
    Apr 2008
    Location
    Portland, Oregon U.S.A.
    Posts
    443
    Thanks
    108
    Thanked 15 Times in 14 Posts

    Count Parent Elements

    Hi I am using a event listener for clicked elements. The event listener limits a bubble up of elements to point to this specifically clicked element before preforming the rest of my function.

    Great it all works fine, but now that I am pointing to my element how would one go about counting the number of specific parent elements this clicked element has?

  • #2
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,511
    Thanks
    3
    Thanked 504 Times in 491 Posts
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
      <title></title>
    <script language="JavaScript" type="text/javascript">
    /*<![CDATA[*/
    
    function Test(obj){
     var cnt=0;
     while(obj.parentNode&&obj.nodeName!='BODY'){
      cnt++;
      obj=obj.parentNode;
     }
     alert(cnt);
    }
    /*]]>*/
    </script></head>
    
    <body>
    <div >
    <div >
    <a onclick="Test(this);">Click Me</a>
    </div>
    </div>
    
    </body>
    
    </html>
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/

  • Users who have thanked vwphillips for this post:

    ubh (12-21-2008)

  • #3
    ubh
    ubh is offline
    Regular Coder ubh's Avatar
    Join Date
    Apr 2008
    Location
    Portland, Oregon U.S.A.
    Posts
    443
    Thanks
    108
    Thanked 15 Times in 14 Posts
    Hey vwphillips,
    as always great code!! This gets me back on track, thanks m8!

    <--Runs to add this to his JS lib

  • #4
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    I don't see the need for a string comparison... document.parentNode === null, so:

    Code:
    function countAncestors(node) {
        if (node.parentNode === null)
            return 0;
        else
            return 1 + countAncestors(node.parentNode);
    }
    Or if you hate recursion:
    Code:
    function countAncestors(node) {
        for (var count = 0; node.parentNode !== null; node = node.parentNode)
            ++ count;
        return count;
    }

  • #5
    Regular Coder
    Join Date
    Jun 2002
    Location
    Australia
    Posts
    197
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Could something like this be used to identify child nodes such as window objects?
    ez
    Animation Rule #64
    Poor quality images are often artistic,
    Poor quality sound is ALWAYS annoying.


  •  

    Posting Permissions

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