SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2007
    Posts
    51
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to get parentNode of a parentNode etc...?

    I need to get the parentNode of a parentNode of a parentNode. Is there a better way to do it than this?

    Code:
    var string = this.parentNode.parentNode.parentNode;

  2. #2
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by rezzner View Post
    I need to get the parentNode of a parentNode of a parentNode. Is there a better way to do it than this?

    Code:
    var string = this.parentNode.parentNode.parentNode;
    It depends from your xhtml code.
    Post it, please ?

    Bye.

  3. #3
    SitePoint Enthusiast
    Join Date
    Nov 2007
    Posts
    51
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here you go:

    Code:
    <div id="div1">
    	<div>
    		<ul>
    			<li><a href="#">link 1</a></li>
    			<li><a href="#">link 2</a></li>
    			<li><a href="#">link 3</a></li>
    		</ul>
    	</div>
    </div>
    
    <div id="div2">
    	<div>
    		<ul>
    			<li><a href="#">link 1</a></li>
    			<li><a href="#">link 2</a></li>
    			<li><a href="#">link 3</a></li>
    		</ul>
    	</div>
    </div>

  4. #4
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think safer
    PHP Code:
    window.onload=function(){
        var 
    eldocument.getElementById('mytest');
        
    el.onclick=function(){
            var 
    lithis.parentNode;
            if(
    li.tagName=='LI'){
                var 
    ulli.parentNode;
                if(
    ul.tagName=='UL'){
                    var 
    divul.parentNode;
                    if(
    div.tagName=='DIV'){
                        var 
    targetdiv.parentNode;
                        if(
    target.tagName=='DIV'){
                            
    alert(target.id);
                        }
                    }
                }
            }
        }

    Bye.

  5. #5
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Are you trying to get the DIV with id="div1" or id="div2" when you click one of the links? Try something like this.
    Code:
    var elem = evt.target | evt.srcElement;
    while (elem.id.indexOf('div') == -1) elem = elem.parentNode;

  6. #6
    SitePoint Enthusiast
    Join Date
    Nov 2007
    Posts
    51
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I need to get the ID of the containing div so events don't pass to both of the divs as some of the inner elements share the same classes/hooks.

  7. #7
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,682
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Try the following, which is more generic and able to be tuned to different needs.

    Code javascript:
    function upToParentByTag(el, tag) {
    	// Climbs the DOM until it gets to the given tag.
    		tag = tag.toUpperCase();
    	el = el.parentNode;
    	while (el.nodeName !== tag && el.nodeName !== 'HTML') {
    		el = el.parentNode;
    	}
    	if (el.modeName === 'HTML') {
    		el = null;
    	}
    	return el;
    }
     
    el = this;
    while ((el.nodeName !== 'DIV' || !el.id) && el !== null) {
    	el = upToParentByTag(el, 'div');
    }
    // el is now the div that has an id attribute
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

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