Does anyone know how to cast in TypeScript?
I\'m trying to do this:
var script:HTMLScriptElement = document.getElementsByName(\"script\")[0];
alert(s
To end up with:
Array object (not a NodeList dressed up as an Array)HTMLElements, not Nodes force-casted to HTMLElementsTry this:
let nodeList : NodeList = document.getElementsByTagName('script');
let elementList : Array = [];
if (nodeList) {
for (let i = 0; i < nodeList.length; i++) {
let node : Node = nodeList[i];
// Make sure it's really an Element
if (node.nodeType == Node.ELEMENT_NODE) {
elementList.push(node as HTMLElement);
}
}
}
Enjoy.