Q:

JavaScript tree

I try to insert into array all the classes
for example
   <div class="class1">

   <div class="class2">

   <div class="class3">

   <div id="class4">

   </div> 
   </div> 
   </div> 

   </div>

And javascript
      var a=document.getElementById("class4");
  var b=new Array(a.parentNode.className,a.parentNode.parentNode.className,
  a.parentNode.parentNode.parentNode.className);
  for(i=0;i<b.length;i++)
  {alert(b[i])}

This is work fine but my question is if I don't know how much parentNode there is to 
the class How can I loop all over the parentNode and insert it into the array? 

A:

​function allParents(node) {
    var arr = [];
    while (node) {
        arr.push(node);
        node = node.parentNode;
    }
    return arr;
}

console.log(allParents(document.getElementsByClassName("class4")[0]));
​

