Categories
JavaScript Answers

How to change element style attribute dynamically using JavaScript?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *