?
Solved

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

Posted on 2011-09-29
7
Medium Priority
?
241 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:Gurvinder Pal Singh
Gurvinder Pal Singh earned 1000 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
PowerShell Core for Advanced Linux Administrators

Understand advanced principals around Powershell Core with a focus on the Linux Administrator.  This course covers how to administer numerous environments across multiple platforms including Linux, Azure, AWS, and Google Cloud from a single shell instance.

 
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 1000 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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

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.
This article discusses how to implement server side field validation and display customized error messages to the client.
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)
Suggested Courses
Course of the Month8 days, 10 hours left to enroll

764 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