43
$(this).css({
    -webkit-transform:'rotate(180deg)',
    -moz-transform: 'rotate(180deg)',
    -o-transform: 'rotate(180deg)',
    -ms-transform: 'rotate(180deg)'
});

This is throwing the error:

Uncaught SyntaxError: Unexpected token -

I'm really hoping I don't have to install the jQuery Rotation plugin just for this one instance.

1

5 Answers 5

84
$(this).css({
    '-webkit-transform':'rotate(180deg)',
    '-moz-transform': 'rotate(180deg)',
    '-o-transform': 'rotate(180deg)',
    '-ms-transform': 'rotate(180deg)'
});
Sign up to request clarification or add additional context in comments.

Comments

80

Quote them:

$(this).css({
    '-webkit-transform': 'rotate(180deg)',
    '-moz-transform':    'rotate(180deg)',
    '-o-transform':      'rotate(180deg)',
    '-ms-transform':     'rotate(180deg)'
});

Comments

25

Just a little addition to the current answers: If you use jQuery 1.8 you don't have to add the prefixes yourself. jQuery will automatically add the appropriate prefix for you.

That means that this code will be enough:

​$(this).css('transform', 'rotate(180deg)');​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​

You won't have to worry about adding new prefixes or removing them if the browser adapted the unprefixed property. :)

Here's a live demo. If you visit the page with a WebKit Browser and inspect the body, you can see that jQuery added -webkit-transform: rotate(180deg); as a style.

Take a look at the Automatic CSS prefixing section here: http://blog.jquery.com/2012/08/09/jquery-1-8-released/

Comments

11

Just to add a little more variety to the list of answers, you can also do

$(this).css({
    WebkitTransform: 'rotate(180deg)',
    MozTransform:    'rotate(180deg)',
    OTransform:      'rotate(180deg)',
    msTransform:     'rotate(180deg)',
    transform:       'rotate(180deg)'
});

Dashes in CSS property names are converted to camelCase in JS for compatibility., so when you use '-webkit-transform' (as in the above examples), jQuery just converts that to WebkitTransform internally.

Comments

2

The new hip way to format commas:

$(this).css({
    '-webkit-transform': 'rotate(180deg)'
    , '-moz-transform':    'rotate(180deg)'
    , '-o-transform':      'rotate(180deg)'
    , '-ms-transform':     'rotate(180deg)'
});

7 Comments

Is there any reason for this? It seems silly, but then most hipster things do :)
I like this formatting, as it allows you to cut and paste lines of code without having to add or remove commas from the previous line.
Except for the first line, whereas the standard formatting lets you cut and paste without adding or removing commas... except for the last line. If it ain't broke...
Fair enough. I see now that the traditional formatting is superior, since the first line and any other lines aside from the last would be interchangeable.
Leading commas is less error prone and easier to manage because most the work happens at the bottom of the stack. Once you get this //commenting a line out is easier, cutting and pasting is easier and seeing the leading comma will help avoid errors by making them more obvious.
|

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.