JS node styles don't get applied if in a if block

Neither style top nor bottom is applied. How can I fix this?

        let btn = document.createElement("button");
        btn.style.position = "fixed";
        if (this.anchorBottom) {
            btn.style.bottom = this.y;
            console.log(true)
        }
        else {
            btn.style.top = this.y;
        }
        btn.style.right = 0;
        document.body.appendChild(btn);```

>Solution :

Provide a unit as well.

let btn = document.createElement("button");
btn.style.position = "fixed";
if (this.anchorBottom) {
  btn.style.bottom = `${this.y}px`;
  console.log(true)
} else {
  btn.style.top = `${this.y}px`;
}
btn.style.right = '0px';
document.body.appendChild(btn);

Remember, you’re not limited to only using px values.

Leave a Reply