Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeScript - Property 'id' does not exist on type 'Node'

I got this issue working with TypeScript (for Angular2) when I try to get the id of an element. See this example:

<div id="search">
    <div id="field1"></div>
    <div id="field2"></div>
</div>

If I try to get the id of one of the nested div, I can do this normally:

var element = document.getElementById("search");
var subelement = element.childNodes;
subelement[1].id;

Then I will have "field2".

However when I try to do this with TypeScript I got the message Property 'id' does not exist on type 'Node'. And as result my code doesn't work. I really need a solution for this since I need to know the nested elements inside another, unless there are a better way.

like image 254
KESO Avatar asked Jul 07 '16 16:07

KESO


1 Answers

Nodes can be more than just elements and don't necessarily have an id property. You should use .children which is a collection of elements only.

like image 191
Daniel A. White Avatar answered Sep 24 '22 19:09

Daniel A. White