Solved

on hover do a check on the background-color of the element

Posted on 2011-09-29
7
234 Views
Last Modified: 2012-05-12
When the .hover( ) fires on element, I want to check what the background-color of that element is.

How can I do this in jQuery?
0
Comment
Question by:knowlton
  • 4
  • 2
7 Comments
 
LVL 15

Expert Comment

by:Eyal
ID: 36818502
0
 
LVL 5

Author Comment

by:knowlton
ID: 36818715
It does not seem to be working properly in my .hover(  )   code:


What happens is when I select a row in a table, the background of that row is highlighted in a color.

If I hover over that row and then leave it....I want the background color to stay as it was, I don't want it to change.  This is okay for rows that are not selected, but not for a selected row.


Here is my current jquery scripting:

var curbkcol = "";

    $('tr:gt(0)', 'table[id*="ShoppingShippingMethods1"]').hover(
                function ()
                {
                    curbkcol = $(this).css("background-color");

                    $(this).css({
                        "background-color": "#FEF8A9",
                        color: "black"
                    });
                },
                function ()
                {
                    $(this).css({
                        "background-color": "white",
                        color: "black"
                    });

                    if (curbkcol == "rgb(254,248,169)")
                    {
                        $(this).css({
                            "background-color": "#FEF8A9",
                            color: "black"
                        });

                    }
                }
                );

Open in new window

0
 
LVL 40

Assisted Solution

by:gurvinder372
gurvinder372 earned 250 total points
ID: 36848909
i guess first you have to define the default color in a class, and then toggle that class with another one in case of hover, like this
http://stackoverflow.com/questions/275891/jquery-hover-and-class-selector

$('tr:gt(0)', 'table[id*="ShoppingShippingMethods1"]').hover(
    function(){
        $(this).css ("background-color", "#FEF8A9");
    },
    function(){
        $(this).css ("background-color", "#fff");
    } );

0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 5

Author Comment

by:knowlton
ID: 36891868
This takes care of the hover effect, but what about when a row is already selected, and then I hover over the row?

Typically the row loses its selection color and returns to the normal color, because that is what the hover function do.  Except in the case of an already selected row, this behavior is not desired.

So how can I have an exception for the already selected row?
0
 
LVL 15

Accepted Solution

by:
Eyal earned 250 total points
ID: 36893651
play with classes and not with css directly
this way you can add/remove hover class when hovering the row
when you will remove the class the old class will apply. if you had there selected class it will stay selected otherwise it will return to unselected
0
 
LVL 5

Author Comment

by:knowlton
ID: 36904503
>>>play with classes and not with css directly

====================

I think this is where I will be heading, once I get more time to try this out.
0
 
LVL 5

Author Closing Comment

by:knowlton
ID: 37008208
thx
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now