The problem arises from the fact that you cannot use a variable as the name of a property within an object using the syntax provided. Instead, you must utilize bracket notation to specify the property and its corresponding value. Consider implementing the following approach:
var cssStyles = { 'position': 'absolute' };
cssStyles[dynamicCssProperty] = menuWidth;
$('nav').css(cssSettings);
Alternatively, you have the option of making two separate calls to the css()
method, assigning each attribute individually:
$('.nav').css('position', 'absolute').css(dynamicCssProperty, menuWidth);