hanu
hanu

Reputation: 63

How to style css top using javascript

I'm trying to set top using java script. I logged the top of the menu after adding top style, but it does not work.

How to style top using js??

Here is my codes

const top = (document.getElementById(element).offsetTop);

top is a number not String

document.getElementById('contentMenu').style.top = top.toString();
    
console.log("menu" +document.getElementById('contentMenu').style.top);   //output: menu

Upvotes: 0

Views: 374

Answers (1)

gugateider
gugateider

Reputation: 2049

const top = document.getElementById(element).getBoundingClientRect().top;
document.getElementById('contentMenu').style.top = `${top}px`;
console.log(document.getElementById('contentMenu').style.top)

Upvotes: 1

Related Questions