0

I have css like so:

.myTabs .JustForFun .ajax__tab_inner:hover 
{
    width: 109px; 
    background:  url ("/images/csc_tr.png") no-repeat 100% 0%; margin-right: 2px;
    background-color: #BBC614; 
}

Using javascript, jquery or asp.net code behind c# I would like to manipulate background-color attribute.

How can I do this, what are the pros and cons of the solution you've recommended me is what I would like to know.

4
  • Are you looking to set that dynamically on each hover, or are you trying to set that value on page load? Commented Jan 26, 2012 at 16:34
  • Have you considered using css3? It will give you nice transitions through the color change although it would not be compatible with older browsers. Commented Jan 26, 2012 at 16:34
  • @kwelch page load :) or dom object ready Commented Jan 26, 2012 at 16:36
  • @JackalopeZero I have considered css3 since it supports variables I am restricted by supporting older browsers for our client base, thats changing so maybe in the future will be cool. Commented Jan 26, 2012 at 16:37

4 Answers 4

4

In jQuery you can simply use

<script type="text/javascript">
    $(document).ready(function(){
        $(".myTabs").css("background-color","your color");
    });
</script>

If you do it via c# code then the page will refresh.

You cannot apply attribute changes to :hover through javascript instead you should use jquery.hover function.

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

1 Comment

So I just do $(".myTabs .JustForFun .ajax__tab_inner:hover").css("background-color","your color");
2

Here it is using css3 (adapted from a w3schools tutorial).

I know its not javascript like you asked for, but this is much easier if you are looking for simple effects.

1 Comment

thanks for the Jackalope the transition parameters are awesome!
1

Here is what I did. It sets the initial value in the css. Then I have the jquery that changes the div on hover.

The first function in the is when the cursor enters and the second is when the cursor exits.

Demo

1 Comment

I will accept you answer I was writing up essentially the same thing haha thanks buddy!
0

any classes with :hover, :active etc cannot be manipulated from javascript or jquery easily enough. The recommended alternative is to drop the :hover css function and us jquery.hover function instead.

jquery.hover:

$(".classOrID").hover(

    function () {
        $(this).css("YourAttribute", <%= primaryColor %>);
    } ,

    function () {
        $(this).css("YourAttribute", <%= secondaryColor %>);

    }
); 

You may notice the <%= primaryColor %> this just gets the value in c# from code behind which picks it up from a color picker in my cms.

For anything with just a css change not requiring :hover I would do.

$('.classOrID').css('background-color', <%= primaryColor %>);

Or what Shoban recommended javascript solution without jquery.

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.