Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery .css() function with variables and multiple values

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

like image 639
technopeasant Avatar asked Dec 11 '11 04:12

technopeasant


People also ask

How can use multiple CSS properties in jQuery?

Apply multiple CSS properties using a single JQuery method CSS( {key1:val1, key2:val2....). You can apply as many properties as you like in a single call. Here you can pass key as property and val as its value as described above.

What is the use of CSS () method in jQuery?

jQuery css() Method The css() method sets or returns one or more style properties for the selected elements. When used to return properties: This method returns the specified CSS property value of the FIRST matched element.

Can jQuery manipulate CSS?

The jQuery CSS methods allow you to manipulate CSS class or style properties of DOM elements. Use the selector to get the reference of an element(s) and then call jQuery css methods to edit it. Important DOM manipulation methods: css(), addClass(), hasClass(), removeClass(), toggleClass() etc.

How can check CSS property value in jQuery?

Get a CSS Property Value You can get the computed value of an element's CSS property by simply passing the property name as a parameter to the css() method. Here's the basic syntax: $(selector). css(“propertyName”);


1 Answers

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

$('h1').css({
    'font-size': victore + "em",
    'color':'red'
});
like image 129
James Montagne Avatar answered Sep 29 '22 01:09

James Montagne