Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular property 'parentNode' does not exist on type 'EventTarget'

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?

like image 935
Lukas Avatar asked Aug 02 '17 07:08

Lukas


People also ask

Does not exist on type EventTarget?

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.

How do you type an event target in TypeScript?

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.


1 Answers

I had to wrap the typecasting in parentheses:

let parent = ( <HTMLElement>event.target ).parentElement;

or using parentNode:

let parent = ( <HTMLElement>( <HTMLElement>event.target ).parentNode );
like image 106
David Stevens Avatar answered Sep 21 '22 21:09

David Stevens