Hi all,

I am animating the bg color of some links. But I have changed the bg color of each link, so they have unique bg colors.

When I use the jq function i normally use, It doesnt work properly because I can only animate back to one specific color, which i had declared in the callback function..

So..I am asking: How do I save the background color of the element on hover, animate it, and change it back to the color saved in the variable:

$(document).ready(function(){
        $("#main_nav ul li a").hover(function() {
        // Doesnt this save the original color of the element being hovered?
var original_color = $(this).css('backgroundColor');

$(this).animate({ backgroundColor: "#069" }, 800);
},function() {
// How do I use the variable with the original color in here? - This doesnt work..
    $(this).animate({ backgroundColor: original_color }, 300);
});
});

Klemme

Recommended Answers

All 2 Replies

I believe it does. The way you defined is inside a
function which means it's a local variable and the only place you can use it is within the function that it's defined in. You need to have a global variable which you can access and change its value anywhere within your program. define the variable outside of your function and use it anywhere.

Thanks for the answer!

Im just a novice at javascript, and would have to ask you how exactly I do that..?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.