Element.matchParent method
Element › matchParent
Official
Matches the selector recursively up the DOM tree.Signature:
matchParent(selector: string, lastParent?: Element | undefined): Element | nullParameters:
| Parameter | Type | Description |
|---|---|---|
| selector | string | The selector to match the parent with. |
| lastParent? | Element | undefined | The last parent to stop matching against. |
Returns: Element | null — The matched element or null if no match is found.
Example:
const element = createEl('p');console.log(element.matchParent('p')); // <p></p>console.log(element.matchParent('strong')); // nullconst child = element.createEl('strong');console.log(child.matchParent('strong')); // <strong></strong>console.log(child.matchParent('p')); // <p></p>const grandchild = child.createEl('em');console.log(grandchild.matchParent('p', child)); // null