How to change css using document.getElementsByClassName in typescript

4.4k views Asked by At

I want to set style top for some elements using typescript

for(var i=0; i< document.getElementsByClassName('calendar-event').length; i++){
        document.getElementsByClassName('calendar-event')[i].style.top = 50*i;
    }

This returns error Property 'style' does not exist on type element. How to make this work in typescript??

1

There are 1 answers

2
Aami On BEST ANSWER

You can use setAttribute as below

let elemets = document.getElementsByClassName('calendar-event');

for(var i=0; i< elements.length; i++){
   var div = elements[i]
   div.setAttribute("style","top:"+50*i+"px");
}