I'm trying to reach a parent of some element:
let t = e.target.parentNode
but i have this Error: Property 'parentNode' does not exist on type 'EventTarget'
I've tried with <Element>e.target.parentNode
but this also not gives me any results. Can anybody help?
The error "Property 'value' does not exist on type 'EventTarget'" occurs when we try to access the value property on an element that has a type of EventTarget . To solve the error, use a type assertion to type the element correctly before accessing the property. This is the index.
Use a type assertion to type event. target in TypeScript, e.g. const target = event. target as HTMLInputElement . Once typed correctly, you can access any element-specific properties on the target variable.
I had to wrap the typecasting in parentheses:
let parent = ( <HTMLElement>event.target ).parentElement;
or using parentNode:
let parent = ( <HTMLElement>( <HTMLElement>event.target ).parentNode );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With