Changing element style attribute dynamically using JavaScript

jslearner picture jslearner · Mar 4, 2011 · Viewed 245.6k times · Source

I hav a certain style sheet for a div. Now i want to modify one attribute of div dynamically using js.

How can i do it?

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

Is this correct?

Answer

KooiInc picture KooiInc · Mar 4, 2011

In addition to other answers, if you want to use the dash notition for style properties, you can also use:

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