Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

adding a css property

Hi, I'm trying to add a css property once a link is clicked, but with no success. Can anyone point me in the right direction, please.

$(document).ready(function(){
    $('nav a').click(function(){
        $(".text_box").css("-webkit-animation":"sliderIn  5s linear");
     });
});

The .text_box is a div, and i have already written the keyframe rule. Thank you

4 Answers

You need to add curly braces around the strings within the css function like so:

// within the click function
// line-break added for clarity
$(".text_box").css( 
   { "-webkit-animation":"sliderIn  5s linear" }
);

Frank Cameron-Wilson -

Alternatively, instead of adding curly braces you can swap out the colon for a comma in the CSS string.

However, you can't mix and match the 2 syntaxes :wink:

...

You can add animation using the css function - http://jsfiddle.net/JVdkv/

Sean T. Unwin - I'm always down for a good demo, they're great for clearing things right up. :guitar:

James Barnett. I concur! :) Note: For anyone viewing the fiddle without a webkit browser you will need to edit the browser prefix in the css and js sections.

tried to edit reply to Kevin...

Yeah, I realized that after I wrote me response. I learned something new today.

Thank you all, glad to have created a buzz around the subject.. ;-) Haven't seen jsfiddle before, top site.

Personally I find codepen to have a far superior user experience over jsfiddle.