Search code examples
javascripthtmlcsstypescriptangular5

Changing style of getElementsByClassName gives compile error


I am trying to change the style of all of the elements which I received by calling the method getElementsByClassName. The thing is that it does work when it has compiled before (I commented these rows out to let it compile), it just says the error in the cmd. After it compiled I just set the lines back to normal, they keep giving errors but are working in the browser. Any thoughts on this weird behavior?

enter image description here

enter image description here

When commented out:

enter image description here

When not commented and giving errors:

enter image description here


Solution

  • I think the problem is with TypeScript. You should try this workaround

    var texts = document.getElementsByClassName("section_text") as HTMLCollectionOf<HTMLElement>;