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'
});
Share:
21,971
technopeasant
Author by

technopeasant

Asking obvious questions.

Updated on March 18, 2020

Comments

  • technopeasant
    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 add em 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 of victore:

    $('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
    technopeasant over 12 years
    Ah 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
    technopeasant over 12 years
    gotcha. made a mistake when I copied over my code. see above edit.. it breaks my code.
  • James Montagne
    James Montagne over 12 years
    As pointed out in the comments, you now have a semicolon that shouldn't be there.
  • Brigand
    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