I have custom element which has an input element that is not a first grade child. I want to get the parent custom element from the input and I tried using input.closest()
but it does not work with custom elements tag names
const card = input.closest("game-card");
I need to get the first parent that is a game card so is there a way to do this with custom elements?
<game-card>
<first-level>
<template shadowrootmode="open">
<input
placeholder="click me"
onclick="console.log(this.closest('game-card'))">
</template>
</first-level>
</game-card>
closest( )
does not escape shadowRoots, so you have to recursively walk "up the DOM" to get the nodes you are after.
Explored and documented at SO Question/Answer:
Custom Element getRootNode.closest() function crossing multiple (parent) shadowDOM boundaries