• A utility function that traverses up the DOM tree from a given HTML element, returning the closest ancestor that matches a specified CSS selector.

    It is a replacement for the jQuery closest() method, providing similar functionality without the need for jQuery.

    Parameters

    • element: HTMLElement

      The starting HTML element from which to begin the search for the closest ancestor.

    • selector: string

      A string containing a CSS selector. This string is used to match the ancestors of the element parameter.

    • Optional includeSelf: boolean

      Optional parameter. If set to true, the function will consider the element parameter as a potential match.

    Returns null | HTMLElement

    The closest HTMLElement that matches the provided CSS selector. If no matching element is found, the function returns null.

    Example

    // Get an element by its GUID
    const childElement = ix.api.dom.getHtml("<ELEMENT-GUID>");

    // Get the closest "div" ancestor of the element with id "child"
    const closestDiv = ix.api.dom.closestParent(childElement, "div#child");

    // Get the closest "div" ancestor of the element with id "child", considering "child" as a potential match
    const closestDiv = closestParent(childElement, "div#child", true);