Add style using javascript?


#1

Hello,
Its possible to add style using javascript?
I have this on the header:

<style>
  #canvas-gradient {
    position: absolute;
    display: block;
    width: 100%;
    height: 100%;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
}
</style>

But I want to know if its possible to add that style using javascript.
Thanks


#2

document.getElementsByTagName('canvas')[0].setAttribute("id","canvas-gradient")
(Note: This will only work for one element, because only one element can have an ID.)


#3

Yes, you can add the style via JavaScript instead. It's just not as convenient as doing it with CSS.

var el = document.querySelector("#canvas-gradient");
el.style.position = "absolute";
el.style.display = "block";
...
el.style.left = "0";

#4

Thanks both! Worked!