How can I store property name as a variable?
Example code:
var propertyName = 'margin-top';
$('elem').css({ propertyName : '10px' });
You can store the argument for css() as a variable, and then add the property to it:
var propertyName = 'margin-top';
var cssArg = {};
cssArg[propertyName] = '10px';
$('elem').css(cssArg);
Edit:
In the future (ES6) you'll be able to do that in one line using computed properies:
$('elem').css({ [propertyName] : '10px'});
But for now, you have to stick with the long way.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With