jQuery .css() function with variables and multiple values
21,971
You shouldn't have the quotes around the whole thing:
$('h1').css({
'font-size': victore + "em",
'color':'red'
});
Comments
-
technopeasant about 4 years
Weird little snafu. I'm using jQuery's
.css()
method to change the size of text (long story; no, I can't use media queries) using a variable and I need to addem
to it. I'm not sure what the syntax is because there are multiple values for the CSS change.To illustrate:
This works perfectly. It adds
em
to the calculated value ofvictore
:$('h1').css('font-size', victore + 'em');
This doesn't:
$('h1').css({ 'font-size':victore + 'em', 'line-height':vignelli + 'em'; });
The
em
needs quotes... but so does the value. Wrapping it in parens didn't work -
technopeasant over 12 yearsAh I see. I made a mistake when I copied the code over.. Please see edited post for a different example of what doesn't work. Tried it in your fiddle and no dice
-
technopeasant over 12 yearsgotcha. made a mistake when I copied over my code. see above edit.. it breaks my code.
-
James Montagne over 12 yearsAs pointed out in the comments, you now have a semicolon that shouldn't be there.
-
Brigand over 12 years@technopeasant, see the updated fiddle. I used pixels instead of ems for the example. EDIT: Here's a version with
em
: jsfiddle.net/sPEVs/2