Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6755443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:24:02+00:00 2026-05-26T13:24:02+00:00

I have the following HTML markup: <body> <div class=wrapper> <div class=head> <p class=title>title</p> <a

  • 0

I have the following HTML markup:

<body>
   <div class="wrapper">
      <div class="head">
         <p class="title">title</p>
         <a href="#" class="logo"></a>
      </div>
   </div>
</body

I need to get something like the following – the “A” element with class “logo” is the seconde childNode of the div element with class “head”. The div element with class “head” is the first childNode of the div element with class “wrapper”, the div element with class “wrapper” is the first childNode of body. Then I would get the location of the link with class “logo” as follows:

var a = [1, 1, 2]

So, if I go in the opposite direction using the array above, I will find the link with class “logo” in the DOM starting from the body element. Could anybody tell me how can this be achieved with JavaScript?

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T13:24:03+00:00Added an answer on May 26, 2026 at 1:24 pm

    Here’s a pair of functions adapted from some existing code of mine to turn a node into an array of nested positions within a node and back again.

    Live demo: http://jsfiddle.net/DSNUv/

    Code:

    function getNodeIndex(node) {
        var i = 0;
        while( (node = node.previousSibling) ) {
            i++;
        }
        return i;
    }
    
    function nodeToPath(node, rootNode) {
        var path = [], n = node;
        rootNode = rootNode || document.documentElement;
        while (n && n != rootNode) {
            path.push(getNodeIndex(n));
            n = n.parentNode;
        }
        return path;
    }
    
    function pathToNode(path, rootNode) {
        rootNode = rootNode || document.documentElement;
        var node = rootNode;
        var i = path.length, nodeIndex;
    
        while (i--) {
            nodeIndex = path[i];
            if (nodeIndex < node.childNodes.length) {
                node = node.childNodes[nodeIndex];
            } else {
                throw new Error("Child node index out of range: " + nodeIndex);
            }
        }
    
        return node;
    }
    

    Example use:

    var a = document.getElementsByTagName("a")[0];
    
    var path = nodeToPath(a, document.body);
    alert(path);
    
    var el = pathToNode(path, document.body);
    alert(el.className);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following html <div id=menu> <ul class=horizMenu> <li id=active><a href=# id=current>About</a></li> <li><a
I have following HTML markup, <div id=subcontent_l> <p> <a href=/membership-packages/><img height=202 width=644 alt= src=http://74.52.72.231/wp-content/uploads/2010/06/banner1.jpg
I have the following HTML markup : <a class=link1 href=javascript:load(0,1);>Click here</a><span class=loader></span> The load
Take the following markup: <!DOCTYPE html> <html> <head> <title>My test for StackOverflow</title> </head> <body>
I have the following html markup on my page, <div id=sig_container> <div id=layer1 class=layer
I have the following HTML markup <div id=contents> <div id=content_nav> something goes here </div>
I have the following html: HTML markup <ul id=test> <li><a href=http://www.yahoo.com>yahoo</a></li> <li><a href=http://www.google.com>Google</a></li> </ul>
I have the following HTML <html xmlns=http://www.w3.org/1999/xhtml > <head runat=server> <title></title> <style> .box {
I have the following HTML markup and I want the menu div tag to
I have the following markup inside a master page <html xmlns=http://www.w3.org/1999/xhtml > <head runat=server>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.