Snippets Collections
function getDef(element, def) {
  var str = ""

  var childs = element.childNodes
  for (var i = 0; i < childs.length; ++i) {
    if (childs[i].nodeType != 3) {
      str += childs[i].nodeName + " " + def + "<br />"
      str += getDef(childs[i], def + 1)
    }
  }

  return str
}


// Example
document.body.innerHTML = getDef(document.body, 0)
star

Wed May 04 2022 12:56:13 GMT+0000 (Coordinated Universal Time) https://stackoverflow.com/questions/33903929/iterate-through-html-dom-and-get-depth

#depth #recursive #tag #childnodes

Save snippets that work with our extensions

Available in the Chrome Web Store Get Firefox Add-on Get VS Code extension