I have the following JavaScript variables:
var fontsize = "12px"
var left= "200px"
var top= "100px"
I know that I can set them to my element iteratively like this:
document.getElementById("myElement").style.top=top
document.getElementById("myElement").style.left=left
Is it possible to set them all together at once, something like this?
document.getElementById("myElement").style = allMyStyle
Make a function to take care of it, and pass it parameters with the styles you want changed..
and call it like this
for the values of the properties inside the propertyObject you can use variables..
You can have individual classes in your css files and then assign the classname to your element
or you can loop through properties of styles as -
Using plain Javascript, you can't set all the styles at once; you need to use single lines for each of them.
However, you don't have to repeat the
document.getElementById(...).style.
code over and over; create an object variable to reference it, and you'll make your code much more readable:...etc. Much easier to read than your example (and frankly, just as easy to read as the jQuery alternative).
(if Javascript had been designed properly, you could also have used the
with
keyword, but that's best left alone, as it can cause some nasty namespace issues)