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 set the style object property’s properties.

For instance, we write

document.getElementById("xyz").style["padding-top"] = "10px";

to get the element we’re styling with getElementById.

Then we set its padding-top CSS property by setting the style["padding-top"] property.

Conclusion

To change element style attribute dynamically using JavaScript, we set the style object property’s properties.

Leave a Reply

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