SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Nov 2005
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Better .parentNode Management

    Hi all,

    I would like to not have to write:
    .parentNode.parentNode.parentNode.parentNode.parentNode

    Instead I would like to just write something like this instead:
    .parentNode(5)
    to represent that there needs to be 5 .parentNode

    Obviously I know what I wrote up there is not correct but I was looking for help.

    About six months ago I found a web page that showed me how to do this but I lost the address

    So any help would be fantastic!

    jfyounce

  2. #2
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This depends on your HTML/XML looks, but offsetParent might maybe help you in some cases.

  3. #3
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could use this:
    Code:
    getParentNode = function($element, $levels) {
      if (typeof($levels) == "undefined") {
        $levels = 1;
      }
      while ($level > 0) {
        $element = $element.parentNode;
        --$level;
      }
      return $element;
    }
    You could also extend HtmlElement.prototype, but extending prototypes of core objects is a bad practise.

  4. #4
    SitePoint Member
    Join Date
    Nov 2005
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for your reply!

    So how would I implement that for this?

    var bigSwatchImg = pThisSwatch.parentNode.parentNode.parentNode.getElementsByTagName('div')[0].getElementsByTagName('img')[0];

  5. #5
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    jfyounce, how about adding ids to your HTML? Would that be possible? If not, how about showing the HTML you refer to? Would be interesting to see what you want to access in its DOM.

  6. #6
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jfyounce View Post
    So how would I implement that for this?
    var bigSwatchImg = getParentNode(pThisSwatch, 3).getElementsByTagName('div')[0].getElementsByTagName('img')[0];
    But Pepejeria is right - it may be that you have a deeper lying problem with your application. If you really need to do such complex navigation, using xpath might be an option.


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
  •