Issue
I'm trying to place some HTML inside a specific div. When I try this in typescript I get this error: Property 'innerHTML' does not exist on type 'Element'
. Basicly, this is the code:
document.body.innerHTML = '<div id="myDiv"><div>'
let myContainer = document.querySelector("#myDiv");
myContainer.innerHTML = '<h1>Test</h1>';
Amazingly, it still works when typescripts compiles, but I'm wondering if typescript is giving me an error, what's the right way to go on assigning innerHTML in this case?
Solution
Use a type assertion to placate the compiler:
let myContainer = <HTMLElement> document.querySelector("#myDiv");
myContainer.innerHTML = '<h1>Test</h1>';
Answered By - Vadim Macagon
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.