0

This is the example:

var side = 'left';

$('div').css({
  position: 'relative',
  side: 50
});

I put variable side instead real property name. I was expecting that this will work, but... :)

So, how to set property name from varaibles value?

0

1 Answer 1

3

The only way is to declare previously your object as a variable...

var options = {
  position: 'relative'
};
options[side] = 50;
$('div').css(options);
Sign up to request clarification or add additional context in comments.

3 Comments

this might not work because 50 is not a valid css property, the unit is missing (for instance px)
@Alex jQuery will use it as pixels by default
i made different experiences... since what version?

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.