0

I have a multi language website, I wanna change my container position according to language. For this I have to change its position accordingly I tried below code.

prop = lang == 'ar' ? 'right' : 'left';
$('#head').css({ prop : 200 });

But I am unable to achieve this, when I put static property then its working. I know there are other ways but Is it not possible like this ?

0

3 Answers 3

3

You can create the object first, and then pass it into the css function.

prop = lang == 'ar' ? 'right' : 'left';
var styles = {};
styles[prop] = 200;
$('#head').css(styles);
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks, this will work. Will mark it as an answer once it allow me.
For the record, in ES6 you can use computed property names.
@DanishAdeel: Sure it works, but why add 2 extra lines of code and the extra complexity when you can just use $('#head').css(prop, "200");? :)
@GoneCoding That's fine until you want to set more than one property at a time.
@GoneCoding I have just checked your answer which is also solved my problem, I wish I can mark both of you as Correct Answer :)
|
2

You are using the version of css that required named properties. Use the other version.

prop = lang == 'ar' ? 'right' : 'left';
$('#head').css(prop, "200");

Comments

1

Have you tried like below:

var prop = lang == 'ar' ? 'right' : 'left';
var cssObj = {};
cssObj[prop] = 200;
$('#head').css(cssObj);

Comments

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.