3

This works OK:

$(container_elm).css({
  'border-width':'1px'
})

Now I am trying to pass CSS propertyName : value as a parameter to a function:

function my_func(css_arr) {
  if (css_arr!==null && css_arr!=='') {
    for(var x in css_arr) {
      if(css_arr.hasOwnProperty(x)) {
        y=css_arr[x];
        x_new='"'+x+'"';
        y_new='"'+y+'"';

        $(container_elm).css({
            //'border-width':'1px' // this works
            //x:y // this does NOT work
            //x_new:y_new // this does NOT work
        });
        //console.log(x_new, y_new); //returns "border-width" "1px"
        //console.log(x, y); //returns border-width 1px
      }
    }
  }
}

//usage
my_func({'border-width':'1px'});

How can I pass CSS object "propertyName : value", and make .css() receive it and work?

Edit

Demo - http://jsfiddle.net/BRwzF/5/

3
  • what's the result of the console.logs? Commented Aug 2, 2012 at 17:17
  • Maybe you could provide a working example in a jsfiddle.com ? Commented Aug 2, 2012 at 17:17
  • SomeKittens: added console.log output to question. Commented Aug 2, 2012 at 17:19

3 Answers 3

2

Instead of mucking about with x and x_new, etc, if you're passing in JSON already, why not do this:

$(container_elm).css(
    css_arr
);
Sign up to request clarification or add additional context in comments.

6 Comments

good idea, but I get "SyntaxError: invalid object initialize" in firebug console
sorry, my fault, yes it works. But any idea why the code with for in loop does not work?
You're using the for...in loop incorrectly. There's no need for it here.
now I see I don't need for...in here. But anyway how should I use correctly in my case?
@ihtus no need for the function altogether just use $(el).css({'border-width':'1px'}). You can pass a complete object and also something like {"left":"+=20"}
|
1

I totally agree with what @Somkittens suggested. Just want to brief you about why x_new:y_new did not work for you.

In .css() jquery function, a key/property does not get parsed or treated as a variable, so putting $('dom').css('width': '100px') or $('dom').css(width: '100px') does not make any difference. We wrap properties in quotes in case it has hyphens in it. i.e. border-radius, -webkit-border-radius etc.

But still if you want to go with your solution, in that case you have tweak your code a bit which does the same thing as @SomeKittens's

 y=css_arr[x];
 x_new=x;
 y_new=y;
 var foobar = {};
     foobar[x_new] = y_new;
 $(container_elm).css(foobar);

Demo: http://jsfiddle.net/4tBVH/

Comments

0

You don't need for to do this.
css_arr is an object with css style. You can pass it directly to the .css().
Example: $(container_elm).css(css_arr);

Suggestion: Change css_arr to style.

demo

1 Comment

thank you. But any idea why the code with for in loop does not work?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.