Snippets Collections
    // 3. side nav link tracking
    function sideNavTracking() {
        const sideNavLinks = document.querySelectorAll('nav.lhs-menu a');
        if (!sideNavLinks.length) return; // fixed
    
        sideNavLinks.forEach(link => {
            link.addEventListener('click', function(e) { // pass e
                maybePreventDefault(e);
    
                // Get only text nodes (exclude SVG)
                const linkTextRaw = Array.from(link.childNodes)
                    .filter(node => node.nodeType === Node.TEXT_NODE)
                    .map(node => node.textContent.trim())
                    .join(' ');
                
                const linkText = linkTextRaw.replace(/\s+/g, ' ').trim() || "";
                const linkUrl = link.getAttribute('href') || "";
    
                let navLevel = 1;
                if(linkText && linkUrl){
                    gtmPush({
                    event: 'navigation_click',
                    navigation_type: 'side_nav',
                    click_text: linkText,
                    click_url: getNormalisedUrl(linkUrl),
                    nav_level: navLevel,
                    }); 
                }
               
            });
        });
    }
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

Thu Feb 12 2026 02:32:32 GMT+0000 (Coordinated Universal Time)

#textcontent #childnodes
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