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.
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:
Example use: