views:

48

answers:

3

First, the disclaimer- I'm JUST starting out with jQuery. I apologize for my ineptness. :)

My issue is that I'm attempting to use a jQuery rollover effect on a bunch of separate elements on a page. Right now, when I mouse over one, they all do the effect at once because they have the same class name, and jQuery is selecting all of them. I know I could use a different name for each, and write a separate bit of jQuery for each class, but is there a better way? I plan on having many of these elements.

Anyway, a picture is worth a thousand words. Check out the source of my test page here: Click Here Mouse over the 'test' images. I want the effect to just apply to the one you mouse over.

Here's my novice jQuery:

    $(document).ready(function(){

$('.box').hover(function() {
 $('.rollover').fadeIn('slow');
 $('.description').fadeIn('fast');
});

$('.box').mouseleave(function() {
 $('.rollover').fadeOut('slow');
 $('.description').fadeOut('fast');
});


});
+3  A: 
$('.box').hover(function() {
   $(this).next('.rollover').fadeIn('slow');
   $(this).find('.description').fadeIn('fast');
}, function() {
   $(this).next('.rollover').fadeOut('slow');
   $(this).find('.description').fadeOut('fast');
});

The .hover() method accepts two functions to fire for mouseenter and mouseleave.

Within each function, the this keyword refers to the .box element that received the event. Then you use .find() to get the nested .description, and .next() to get the adjacent sibling .rollover.

In your situation, you could use .siblings('.rollover') instead of .next() if you want.

patrick dw
+1, but it would be nice to add an explanation, as it's such a good illustration of what `this` does.
chryss
@chryss - Yes, I had already updated my answer with an explanation of what `this` represents. Was that what you meant?
patrick dw
Careful with the `this` keyword; it doesn't always resolve how you would think.
palswim
@palswim - In this sort of situation, it is perfectly safe to rely on `this` to represent the DOM element that received the event.
patrick dw
@patrick - yes: it wasn't visible to me at that point in time on SO. I'm sure some updates are shown first to the author before they ripple down into the main database, if such a thing exists.
chryss
A: 

If I could select them one at a time, I think I could apply the effect only to their children. I just wish you could tell jquery to just select whatever your mouse is over without stating an ID or Class...

KThornbloom
A: 

Just wanted to say thank you to patrick dw! It works perfectly, and I appreciate the explanation behind it as well. (Sorry for posting this as an answer, I'm new so I don't have enough rep points to reply.)

KThornbloom
@user - You're welcome, although here on SO, it is typical to click the big checkmark to the left of one answer to confirm that it helped you resolve the issue. You get a couple reputation points when you do that too. :o) Although, you'll need to log in to the original account. http://stackoverflow.com/users/422165/kthornbloom
patrick dw
@patrick dw Crap, I originally asked the question as an unregistered user. If there's a way to get back in, I'll gladly get you those points!
KThornbloom
@KThombloom - Not a big deal. If this link: http://stackoverflow.com/users/422165/kthornbloom ...doesn't work then don't worry about it. :o)
patrick dw