1

I have a div with a css style of say green or blue, depending on the nth child color. I set it to orange using

<div id='yo' class='alternatingcolors' style='background-color: #FF9900;'>hello</div> 

and I want to animate it back to its original color using jquery's

$('yo').animate({ backgroundColor: 'transparent'}, 500);

However, neither transparent, inherit, null, or '' work.

How would I get it back to its original color? Essentially I want to animate it to the state before: style='background-color: #FF9900;' was set.

2 Answers 2

1

The lookup to 'yo' should be prefixed with a hash, e.g.:

$('#yo')

If that's not the issue:

You could store the original colour as data in the div, then restore it later:

<div id='yo' data-original-color='#FF9900' class='alternatingcolors'>

then:

$('#yo').animate({ backgroundColor: $('#yo').data('original-color') }, 500);
Sign up to request clarification or add additional context in comments.

2 Comments

Woops, forgot the hash (typed it, not copy pasted). Is there any way to do it without storing it?
As far as I'm aware, once you change a property (e.g. background-color) of an element, it is kept that way and there is no handy reference to the original.
0

you can save its first background-color into a variable and then use it:

orange = $("yo").css("background-color")

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.