0

I have this code:

$.each(properties, function(i, key) {
   obj.css({'-webkit-border-' + key + '-radius': value+'px', '-moz-border-' + key + '-radius': value+'px', 'border-' + key + '-radius': value+'px'});
});

It's giving the error on the first + key

Can't I create keys like this (with appending data) or am I doing something else wrong?

Thanks, Wesley

0

2 Answers 2

1

Can't I create keys like this (with appending data) or am I doing something else wrong?

Nope, you should be getting a syntax error.

You can construct the object before passing it to the css function:

var styles = {};
styles['-webkit-border-' + key + '-radius'] = value+'px';
styles['-moz-border-' + key + '-radius'] = value+'px';
styles['border-' + key + '-radius'] = value+'px';
obj.css(styles);
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, that seems to do the trick, though I now have another error to fix ;) (somewhere else)
+1 The property must be "either a name, a number, or a string literal" - See reference
Can you look at the edits I made to the question, it seems the dynamically generated a, b, c don't work for some reason with .css()
1

try this

$.each(properties, function(i, key) {
   var a = '-webkit-border-' + key + '-radius';
   var b = '-moz-border-' + key + '-radius';
   var c = 'border-' + key + '-radius';
   var z = value+'px';
   obj.css({a : z, b: z, c: z});
});

I think that json don't like "building" its keys and values directly

1 Comment

Actually, there is no JSON involved anywhere on this question :)

Your Answer

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