1

I am trying to simply animate the background on a keystroke. I can't get it to work using:

$('input#userInput').live('keydown', function() { 
    $(this).animate({backgroundColor:'#4E1402'}, 300).delay(800).animate({backgroundColor: '#555'}, 100);
});

Please let me know where I am going wrong. Thanks!

2
  • ok, I see what everyone is saying about jquery UI...let's just use color for example...is the code correct then for color? I can't get that! jsfiddle.net/6k6uK Commented Sep 15, 2012 at 4:44
  • I included a jsFiddle in my answer that uses your exact code with the jQuery.color() plugin. Commented Sep 15, 2012 at 4:46

3 Answers 3

2

That's because you can't animate background-color without a plugin

All animated properties should be animated to a single numeric value, except as noted below; most properties that are non-numeric cannot be animated using basic jQuery functionality (For example, width, height, or left can be animated but background-color cannot be, unless the jQuery.Color() plugin is used). Property values are treated as a number of pixels unless otherwise specified. The units em and % can be specified where applicable.

jsFiddle with the plugin

Sign up to request clarification or add additional context in comments.

2 Comments

@webwrks - Appologies... forgot to hit the update button after I added the plugin. Should be working now. I updated the link.
thanks! I liked the yo! touch but was wondering too. I wonder if this is something jQuery will ever make possible w/out the UI. Thanks again.
1

Try the color plugin, which "Adds the ability to do color animations to jQuery 1.2, and newer."

Also, jQuery UI already includes color animation support.

Comments

1

Try like this

$('#userInput').keydown(function(){
  $(this).css("backgroundColor",'#4E1402');
$(this).delay(2000).queue(function(){
    $(this).css("backgroundColor",'#555');});
 });

1 Comment

@Gautam3164 - There is nothing wrong with his syntax... jQuery simply doesn't support animating background-color using the core library.

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.