6

I'd like to set css of a div element dynamically using jQuery css() function instead of using string literals/ string constants for the css() function. Is it possible?

Instead of using the following codes with string literals:

$('#myDiv').css('color', '#00ff00');

I would like to use variables to set css for #myDiv element like

Version 1:

var propertyName = get_propery_name(myVariable1); // function get_propery_name() returns a string like 'background-color'
var value = get_value(myVariable2) ; //  function get_value() returns a string like '#00ff00'
$('#myDiv').css(propertyName, value);

Version 2: (just hard coded to see if they work without calling custom functions like version 1 above):

var propertyName = 'background-color';
var value = '#00ff00';
$('#divLeftReportView').css(propertyName, value);

Both variable versions of codes do not work. Please help. Thanks.

2
  • 1
    There is nothing special in it. Both variants should work well: jsfiddle.net/UQWyH Commented May 16, 2012 at 14:03
  • Did this answer your question? Please accept an answer if one of them helped you Commented Aug 8, 2012 at 20:12

5 Answers 5

2

Both of your examples will work just fine. I would suggest just a bit cleaner approach (personal syntax preference):

$(document).ready(function () {
    $('#myDiv').css(get_propery_name(myVariable1), get_value(myVariable2));
}

Here's a working fiddle.

If you want to take it a step further, you can return a CSS map instead of strings:

$('#divLeftReportView').css(GetCssMap("foo"));

function GetCssMap(mapIdentifier) {
    return { "background-color" : "#00ff00" }
}

Here's a working fiddle.

Sign up to request clarification or add additional context in comments.

Comments

1

The code you posted here should work. I have done both versions of what you are trying to do several times. If it is not working, there is a good chance that something is wrong somewhere else in your javascript OR that you do not have the correct selector/id for the element(s) which you want to change.

Try adding alert("test"); immediately after $('#divLeftReportView').css(propertyName, value);. If you get the popup saying "test" then the problem is with your selector. If not, then the problem is a bug in your javascript.

Also try adding $('#divLeftReportView').css("background-color", "#00ff00"); in the same place. That will confirm whether or not the selector is working.

1 Comment

Thanks. I found the problem is I use upper case for property name like BACKGOUND-COLOR for jQuery css() method, so my codes did not work with things like $('#divLeftReportView').css("BACKGOUND-COLOR", "#00ff00"); It turns out the property name must be lower case. So, the codes should be $('#divLeftReportView').css("background-color", "#00ff00");
1

Seems to work fine at http://jsfiddle.net/gaby/6wHtW/

Make sure you run your code after the DOM ready event..

$(function(){
    var propertyName = 'background-color';
    var value = '#00ff00';
    $('#divLeftReportView').css(propertyName, value);
});

otherwise your elements might not be present in the DOM..

2 Comments

What if the value for the 'value' variable is got from an input textbox (user enters) like: var value = $('#myTextBox').val(); ? What I mean is how the value of 'value' variable is converted to a string literal or string constant?
since it is a string it will work as is... whether you pass a string literal or a variable holding a string it is the same thing..
1

You can also pass multiple CSS parameters within one variable as an array:

$(function(){
    var divStyle = {'background-color': '#00ff00', 'color': '#000000'}
    $('#divID').css(divStyle);
});

Comments

0

yes, using jQuery attr method you can change css dynamically

var height=$(".sampleClass1").innerHeight();
$('.sammpleClass2').attr('style', 'min-height:'+height+' !important');

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.