Categories
interview

DFS DOM (ES6)

<div id="root">
  <div>
    <ul>
      <li>1</li>
      <li>2</li>
      <li>3</li>
    </ul>
  </div>
  <div>
    <ul>
      <li>1</li>
      <li>2</li>
      <li>3</li>
    </ul>
  </div>
</div>
const root = document.getElementById("root");

const walk = function (root) {
  let obj = root.children;
  for (let key in obj) {
    if (obj.hasOwnProperty(key)) {
      if (obj[key].textContent === "1") 
        console.log(obj[key]);
      else 
        walk(obj[key]);
    }
  }
};

walk(root);