title: Get DOM path from a node to another
body: <p>I need a method that, taken as parameters two nodes (<code>node1</code> and <code>node2</code>), returns the minimum path that leads to <code>node2</code> from <code>node1</code>.
  Ideally, it returns an array of nodes, but for the moment it's OK to a string. So for example:</p>
  
  <pre><code>        P
        /   \
    #text    U
           /   \
          B     I
          |     |
       #text   #text
  
  function foo(node1, node2) {
      ...
  }
  </code></pre>
  
  <p>when I run it in this way, for example on the nodes <code>P</code> (root) and <code>B</code>:</p>
  
  <pre><code>var res = foo(P, B);
  console.log(res);
  </code></pre>
  
  <p>I obtain:</p>
  
  <pre><code>res = Array[3] {
      0: P (class=..., id=...)
      1: U (class=..., id=...)
      2: B (class=..., id=...)
  }
  </code></pre>
  
  <p>or, in the form of string:</p>
  
  <pre><code>res = "P(class=..., id=...) &gt; U(class=..., id=...) &gt; B(class=..., id=...)";
  </code></pre>
  
  <p>If the nodes have attributes (such as <code>id</code> or <code>class</code>), then returns even those (as in the example).
  I searched the internet methods that did similar things but I found only methods that return the full path of the entire document and not between two nodes.
  For example, I tried this method doesn't work for me because it returns the full path of a single node.</p>
  
  <pre><code>function getDomPath(el) {
    var stack = [];
    while ( el.parentNode != null ) {
      console.log(el.nodeName);
      var sibCount = 0;
      var sibIndex = 0;
      for ( var i = 0; i &lt; el.parentNode.childNodes.length; i++ ) {
        var sib = el.parentNode.childNodes[i];
        if ( sib.nodeName == el.nodeName ) {
          if ( sib === el ) {
            sibIndex = sibCount;
          }
          sibCount++;
        }
      }
      if ( el.hasAttribute('id') &amp;&amp; el.id != '' ) {
        stack.unshift(el.nodeName.toLowerCase() + '#' + el.id);
      } else if ( sibCount &gt; 1 ) {
        stack.unshift(el.nodeName.toLowerCase() + ':eq(' + sibIndex + ')');
      } else {
        stack.unshift(el.nodeName.toLowerCase());
      }
      el = el.parentNode;
    }
  
    return stack.slice(1); // removes the html element
  }
  </code></pre>
  
  <p>Another thing, I would use pure JavaScript, no jQuery.
  I have no idea how to do what I need, a your help would be greatly appreciated.</p>
  
  <p>Thank you</p>