views:

63

answers:

8

I have the following dynamic variables:

var fontsize = "12px"
var left= "200px"
var top= "100px"

The variables may differ, it may be also others like color, line-height... I know that I can set them to my element like this:

document.getElementById("myElement").style.top=top
document.getElementById("myElement").style.left=left

...but is it possible to set them all together, something like:

document.getElementById("myElement").style = allMyStyle 

Thank you.

+1  A: 

A Javascript framework allows you to do these things very easily

jQuery

$('#myElement').css({
  fontsize: '12px',
  left: '200px',
  top: '100px'
});

Object and a for-in-loop

Or, a much more elegant method is a basic object & for-loop

var el = document.getElementById('#myElement'),
    css = {
      fontsize: '12px',
      left: '200px',
      top: '100px'
    };  

for(i in css){
   el.style[i] = css[i];
}
Harmen
Yes, but on this project I can not use jQuery...Thanx
Mircea
A: 

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 -

var css = { "font-size": "12px", "left": "200px", "top": "100px" };

for(var prop in css) {
  document.getElementById("myId").style[prop] = css[prop];
}
Sachin Shanbhag
That is not an option sice the variables are dynamic
Mircea
This isn't exactly what the user asked, but it is most likely the most viable solution. +1
Ryan Kinal
A: 

See for .. in

Example:

var myStyle = {};
myStyle.fontsize = "12px";
myStyle.left= "200px";
myStyle.top= "100px";
var elem = document.getElementById("myElement");
var elemStyle = elem.style;
for(var prop in myStyle) {
  elemStyle[prop] = myStyle[prop];
}
Onkelborg
A: 

Don't think it is possible as such.

But you could create an object out of the style definitions and just loop through them.

var allMyStyle = {
  fontsize: '12px',
  left: '200px',
  top: '100px'
};

for (i in allMyStyle)
  document.getElementById("myElement").style[i] = allMyStyle[i];

To develop further, make a function for it:

function setStyles(element, styles) {
  for (i in styles)
    element.style[i] = styles[i];
}

setStyles(document.getElementById("myElement"), allMyStyle);
nnevala
A: 

Your best bet may be to create a function that sets styles on your own:

var setStyle = function(p_elem, p_styles)
{
    var s;
    for (s in p_styles)
    {
        p_elem.style[s] = p_styles[s];
    }
}

setStyle(myDiv, {'color': '#F00', 'backgroundColor': '#000'});
setStyle(myDiv, {'color': mycolorvar, 'backgroundColor': mybgvar});

Note that you will still have to use the javascript-compatible property names (hence backgroundColor)

Ryan Kinal
+2  A: 

Make a function to take care of it, and pass it parameters with the styles you want changed..

function setStyle( objId, propertyObject )
{
 var elem = document.getElementById(objId);
 for (var property in propertyObject)
    elem.style[property] = propertyObject[property];
}

and call it like this

setStyle('myElement', {'fontsize':'12px', 'left':'200px'});

for the values of the properties inside the propertyObject you can use variables..

Gaby
@Ryan Kinal, thanks for the edit..
Gaby
No problem :-))
Ryan Kinal
A: 

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:

var obj=document.getElementById("myElement").style;
obj.top=top;
obj.left=left;

...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)

Spudley
That is not true, you can set all styles at once via `cssText`.
Felix Kling
@Felix: `cssText` is fine for setting the inline stylesheet value. However if you've also got classes, or if you only want to override some values but not all, it can be quite hard to use `cssText`. So you're right; you can do it, but I'd recommend against it for most use cases.
Spudley
@Spudley: Well having classes is no argument... `obj.top` or `obj.style.color` also justs sets the inline stylesheet value. And yes, in my answer I said that one would overwrite the values... it depends on the context if it is useful or not.
Felix Kling
You better make that obj=document.getElementById("myElement").style; or set your properties obj.style.top=top.
kennebec
@kennebec - hmm, could have sworn I did that. oh well, edited. thanks for spotting.
Spudley
+2  A: 

If you have the CSS values as string and there is no other CSS already set for the element (or you don't care about overwriting), make us of the cssText property:

document.getElementById("myElement").style.cssText = cssString;

This is good in a sense as it avoids repainting the element every time you change a property (you change them all "at once" somehow).

On the other side, you would have to build the string first.

Felix Kling
document.getElementById("myElement").style.cssText +=';'+ cssString; will return a 'normalized' value for element.style.cssText- the new string will replace existing named properties with any new values, add new ones and leave the rest alone.
kennebec
@kennebec: Just tried it and you are right. I didn't know that, I just thought it appends to the already existing text. But it indeed replaces the values...
Felix Kling
@Felix: I hoped somebody would get that and use it- I've never seen it documented. Good answer, by the way.
kennebec
@kennebec Brilliant!
Mircea