11

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

1
  • 2
    On your most recent edit you need to remove the ; after the last em. Commented Dec 11, 2011 at 5:10

3 Answers 3

19

You shouldn't have the quotes around the whole thing:

$('h1').css({
    'font-size': victore + "em",
    'color':'red'
});
Sign up to request clarification or add additional context in comments.

2 Comments

gotcha. made a mistake when I copied over my code. see above edit.. it breaks my code.
As pointed out in the comments, you now have a semicolon that shouldn't be there.
1

Here's a fiddle.

$('h1').css({
    'font-size':victore + "em",
    'color':'red'
});

2 Comments

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, see the updated fiddle. I used pixels instead of ems for the example. EDIT: Here's a version with em: jsfiddle.net/sPEVs/2
0

The font-size value is required to be a string, but that string can be the result of an expression, in your case victore + 'em' should result in the appropriate string.

$('h1').css({ 
    'font-size': victore + 'em', 
    'color': 'red' 
});

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.