Skip to content

Element.matchParent method

Element › matchParent

Official

Matches the selector recursively up the DOM tree.

Signature:

matchParent(selector: string, lastParent?: Element | undefined): Element | null

Parameters:

Parameter Type Description
selector string The selector to match the parent with.
lastParent? Element | undefined The last parent to stop matching against.

Returns: Element | nullThe 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')); // null
const 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