Q:

Making object chain of element by adding parent name

I want to make chain that contain parent element and return me any attributes of top parent (class or id) by using there respective symbol (. and #). I have tried it but ran to infinity loop
html structure:- 
<div id="main">
   <p>Hello Alisha..</p>
   <div class="container">
     <div id="toplayer">

     </div>
     <div class="mid_layer">
         <ul>
            <li>javascript</li>
            <li id="target">qbasic</li>
         </ul>
     </div>
  </div>
</div>

My js code:-
var t = document.getElementById("target");

 function  chain(el) {
    var f = {};
    var m = el;
    if (el !== null && el !== undefined) {
      var y = m.parentElement;
      while(null !== y){
        var r = {};
        r['parent'] = y;
        f[m.tagName] = r;
        var m = y;
      }
      return f;
     }
   }
  chain(t);

Loop dosn't stop in above program.
Please don't suggest me idea using jquery

A:

Move the assignment of y inside the loop (as DrC commented), as it is now y is always not null, hence the infinity loop. 
var t = document.getElementById("target");

 function  chain(el) {
    var f = {};
    var m = el;
    if (el !== null && el !== undefined) {
      var y = m.parentElement;
      while(null !== y){
      y = m.parentElement;
        var r = {};
        r['parent'] = y;
        f[m.tagName] = r;
        var m = y;
      }
      return f;
     }
   }
  chain(t);

