1

I am new to jQuery and trying to figure out how to use the .hover(); effect in place of the .mouseover() and .mouseout() effects. Here is the scenerio:

I am using a table and want to highlight the row with a CSS class that changes the background color to a light yellow. Here is the CSS:

.bgHvr {
  background-color: #FFFACD;
}

The only way I know how to add the hover effect I desire is:

jQuery(document).ready(function($) {
  $('#table tbody tr').mouseover(function() {
    $(this).addClass('bgHvr');
  });
  $('#table tbody tr').mouseout(function() {
    $(this).removeClass('bgHvr');
  });
});

UPDATE:

After some more help I found out a simpler way of a hover effect:

$('#table tbody tr').hover(function() { 
  $(this).toggleClass('bgHvr');
});

This is possible after the 1.4.2 update to jQuery which changed how the .toggle() effect worked.

0

4 Answers 4

4

Hover basically combines the 2 events into one.

.hover(mouseover,mouseout) 

or in your case:

jQuery(document).ready(function($) {
  $('#table tbody tr').hover(function() {
    $(this).addClass('bgHvr');
  },function() {
    $(this).removeClass('bgHvr');
  });
});
Sign up to request clarification or add additional context in comments.

1 Comment

ok thank you, now I understand. each function simply calls the "mouseover" and "mouseout" that is contained within .hover.
2

Um...How about just using CSS?

#table tbody tr:hover {
    background-colour: #FFFACD;
}

EDIT: With DEMO

7 Comments

It isn't fully supported in all versions of IE
this is way more efficient. and supported by everything from IE 7 and up.
Fair enough but anything before IE7 shouldn't be supported anyway, in my opinion. IE6 is 10 years old and should be completely disregarded really.
@anothershrubery I agree with that comment completely. Unfortunately it isn't always a decission we can make as a coder. If something needs to be supported from IE6 onwards, then it has to be coded as such. Personally, I agree with everything you are saying.
I too want to drop support for browsers that don't follow CSS2/3 or HTML5. Anyhow, since this is simply for effect you could get away with this depending how important the hover effect is.
|
1
$('#table tbody tr').hover(
    function() {
        $(this).addClass('bgHvr');
    }, function() {
        $(this).removeClass('bgHvr');
    }
);

Comments

1

Best way I know how and fully compatible with all browsers.

On an unordered list -

JS

$('ul').delegate('li', 'mouseenter mouseleave', function() {
  $(this).toggleClass('hover');
});

CSS

li.hover {
  background-color: #CCC;
}

8 Comments

I was not aware of the .toggleClass function. thank you for showing me this.
No problem! A lot of things toggle in jQuery - instead of show()/hide() think about when you can use .toggle() instead.
though out of curiosity why use the .delegate function and specify the child element? Could you also do this? $('ul li').hover(function() {$(this).toggleClass('hover');});
Yep you absolutely could do that. Your way binds the event handler directly to the LI element. The delegate method uses "event delegation" and binds only one handler to the UL (the delegate) which acts on the LI elements. So if you had 1,000 LI elements, using your way you'd bind 1000 handlers but event delegation uses only 1. It's probably not necessary if you only have a handful of LI elements. Hope that helps!
so if im targeting a common element it would be safer to use delegation so it doesn't tax the javascript engine of the browser with all the handlers for the event?
|

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.