views:

44

answers:

3

I have this:

HTML:

<p class="Link"><a href="...">Test1</a></p>
<p class="Link"><a href="...">Test2</a></p>
<p class="Link"><a href="...">Test3</a></p>

jQuery

$(document).ready(function() {
    $('.Link').hover(function() {
        $('.Link a').css('color', 'black');
    }, function() {
        $('.Link a').css('color', 'white');
    });
});

First of all, I need to change the anchor color when the mouse hovers over the paragraph, not just the anchor. Secondly, I need to do this without creating a unique id for each paragraph or anchor. With the code as it is, it changes the color for all three anchors tags. I only want it to change the color on the anchor contained within the paragraph I am currently hovering over. Is this possible?

Thanks!

+1  A: 

You need to use this which refers to the specific element that received the event.

$(document).ready(function() {
    $('.Link').hover(function() {
             // Get the <a> element from within the context of
             //    the element that received the event, represented
             //    by "this" 
        $('a',this).css('color', 'black');
    }, function() {
        $('a',this).css('color', 'white');
    });
});

Doing:

$('a',this).css('color', 'black');

is effectively the same as doing:

$(this).find('a').css('color', 'black');

Of course, you could always accomplish this using purely CSS.


EDIT:

If all you're doing is changing some CSS attributes, you don't really need javascript.

To use a purely CSS approach, do this:

.Link a {
    color: black;
}

.Link a:hover {
    color: white;
}

Because you're doing this on an <a> element, it is supported on IE6. Starting with IE7 (and most other browsers) you can use the same technique on other elements too.

patrick dw
Could you please elaborate on your last statement?
Albion
@Albion - If you're only changing a CSS attribute, then the `:hover` pseudo selector accomplishes the same thing. If you can accomplish what you need without javascript, then that is best. I'll update my answer at the bottom with an example.
patrick dw
@patrick: Unfortunately I can't set margins on an anchor tag so I had to surround it with a paragraph to create the 3D button press effect I was going for. Since I change the margins in the paragraph and the color/text-decoration in the anchor on hover, and the paragraph is wider then the anchor, my margins change a split second before the color. It was weird on the eyes. So I added the jQuery to change the margins, color and text-decoration when the mouse is over either then <p> or the <a>. You can see the effect at http://test.stanektool.com above the three product images.
Albion
Ok, now I feel stupid. It seems that my color change was wrong. To create the right look I don't want to change the color when the button is depressed. You were right patrick, I can do the entire thing with CSS. But, thanks for the lesson in "this". It taught me a lot.
Albion
@Albion - You're welcome. :o)
patrick dw
A: 
$(this).find("a").css("color", "black");

should do the trick.

The problem is, that ".Link a" matches ALL anchors that are in a paragraph. You probably should read into CSS again with that kind of misunderstanding!

ApoY2k
I'm no guru, I'm just learning.
Albion
A: 

You can use the selector $(this) inside the event to quickly refer to the element which is hovered over. After that you can use .find() to find any elements inside it.

Code:

$(document).ready(function() {
    $('.Link').hover(function() {
        $(this).css('color', 'black');
        $(this).find("a").css('color', 'black');

    }, function() {
        $(this).css('color', 'white');
        $(this).find("a").css('color', 'white');
    });
});
Fabian