Solved

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

Posted on 2011-09-29
7
239 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:Tom Knowlton
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 15

Expert Comment

by:Eyal
ID: 36818502
0
 
LVL 5

Author Comment

by:Tom 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Author Comment

by:Tom 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:Tom 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:Tom Knowlton
ID: 37008208
thx
0

Featured Post

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
This article discusses how to create an extensible mechanism for linked drop downs.
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)
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…

730 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