Sometimes, we want to change element style attribute dynamically using JavaScript.
In this article, we’ll look at how to change element style attribute dynamically using JavaScript.
How to change element style attribute dynamically using JavaScript?
To change element style attribute dynamically using JavaScript, we can set the property in the element’s style
property.
For instance, we write
document.getElementById("xyz").style["padding-top"] = "10px";
to select the element with ID xyz
with
document.getElementById("xyz")
Then we set its padding-top
style to '10px'
by setting its style["padding-top"]
property.
Conclusion
To change element style attribute dynamically using JavaScript, we can set the property in the element’s style
property.