Solved

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

Posted on 2011-09-29
7
235 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

by Julian Matz As of jQuery (http://jquery.com/) 1.4 the .live() method (http://api.jquery.com/live/) supports custom events as well as some standard JavaScript events that it previously didn't support. Among these is the submit event. Unlike …
In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

911 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

16 Experts available now in Live!

Get 1:1 Help Now