Typescript children on Nodes

Viewed 8529

In Javascript this Code works like a acharm, in Typescript I get the following Error:

Property 'children' does not exist on type 'Node'.

This is my code

    var parser = new DOMParser();
    var res = parser.parseFromString(xmldata, "text/xml")
    var branches = res.getElementsByTagName("Branch")
    branches[i].childNodes[7].children
2 Answers

This should work with typescript

const parentEl = document.getElementById('parentId')
if (!parentEl) {
  return
}

const childElements = Object.values(parentEl.childNodes) as HTMLElement[]
for (const childEl of childElements) {
  console.log(childEl.offsetLeft)
}
Related