UMD Web Utilities Library
    Preparing search index...

    Function findParent

    • Recursively finds a parent element with a specified attribute

      Traverses up the DOM tree from the given element until it finds a parent with the specified attribute, or reaches the root.

      Parameters

      • element: { attr: string; element: HTMLElement }

        The starting element

      Returns HTMLElement

      The parent element with the attribute, or the original element if not found

      const button = document.querySelector('button');
      const dialog = findParent({ element: button, attr: 'role' });
      // Returns the closest ancestor with a 'role' attribute