1

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??

0

1 Answer 1

4

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");
}
Sign up to request clarification or add additional context in comments.

2 Comments

Why would you not fix the code so as not to call getElementsByClassName over and over again/
removed the duplication.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.