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
set multiple css style properties in Javascript
or
Example:
See for .. in
Example:
There are scenarios where using CSS alongside javascript might make more sense with such a problem. Take a look at the following code:
This simply switches between CSS classes and solves so many problems related with overriding styles. It even makes your code more tidy.
@Mircea: It is very much easy to set the multiple styles for an element in a single statement. It doesn't effect the existing properties and avoids the complexity of going for loops or plugins.
BE CAREFUL: If, later on, you use this method to add or alter style properties, the previous properties set using 'setAttribute' will be erased.
Don't think it is possible as such.
But you could create an object out of the style definitions and just loop through them.
To develop further, make a function for it:
With Zam you would do it like this