views:

269

answers:

5

Hello

I have two divs. One is position:absolute (.buttonEffectWrapper), over the top of the other div (called .rightColumnButtonHighlighted).

I also have the following JS code:

$(function(){
 $('.buttonEffectWrapper')
  .mouseover(function(){
   $('.rightColumnButtonHighlighted').stop().animate({opacity: 1}, {duration:300})
  })
  .mouseout(function(){
     $('.rightColumnButtonHighlighted').stop().animate({opacity: 0}, {duration:300})
  })
});

It works fine, except it applies to all the divs. I need it to only apply to the current div, and I am not sure how to do that.

DIVS:

<div class="buttonEffectWrapper"></div>
<div id="rightColumnButtonText" >CAR SERVICE</div>
<div id="car-service-highlighted" class="rightColumnButtonHighlighted"></div>

<div class="buttonEffectWrapper"></div>
<div id="rightColumnButtonText" >TRAILER HAULING</div>
<div id="trailer-hauling-highlighted" class="rightColumnButtonHighlighted"></div>

See http://www.raceramps.com/v2 for the effect.

A: 

Use this:

$(function(){
    $( '.buttonEffectWrapper' ).mouseover ( function () {
        $( this ).next ( '.rightColumnButtonHighlighted' ).stop ().animate ( { opacity: 1 }, { duration: 300 } );
    } ).mouseout ( function () {
        $( this ).next ( '.rightColumnButtonHighlighted' ).stop ().animate ( { opacity: 0 }, { duration: 300 } );
    } );
} );

You ware attaching a "hover" event handler to every matched DIV which is OK, but then in your functions you animated all the DIVs not just the current one. $(this) inside the function refers to the current element.

ps: you can use the hover helper in jQuery to make the code a little bit more readable:

$(function(){
    $( '.buttonEffectWrapper' ).hover (
        function () {
            $( this ).next ( '.rightColumnButtonHighlighted' ).stop ().animate ( { opacity: 1 }, { duration: 300 } );
        },
        function () {
            $( this ).next ( '.rightColumnButtonHighlighted' ).stop ().animate ( { opacity: 0 }, { duration: 300 } );
        }
    );
} );

I've updated the code. Use next() to get the next element after $(this)

Jan Hančič
thank you for the info about $(this). Unfortunately, I was looking more for the effect to be triggered by .buttonEffectWrapper, but be applied to the sibling .rightColumnButtonHighlighted.
Jared
I have updated the code
Jan Hančič
A: 

It's because you're selecting all of the elements with class "rightColumnButtonText". You need to do a $(this).siblings('.rightColumnButtonText').stop().animate({opacity: 1}, {duration:300})

blesh
this may get me on the right track. It's actually a sibling, not a child. there is a sibling () right?
Jared
siblings() is what you're looking for.
blesh
A: 
<script type="text/javascript">

$(function(){

    $('.buttonEffectWrapper')

        .mouseover(function(){
            $(this).sibling('.rightColumnButtonHighlighted').stop().animate({opacity: 1}, {duration:300}
        })
        .mouseout(function(){
                    $(this).sibling('.rightColumnButtonHighlighted').stop().animate({opacity: 0}, {duration:300}
        })
});
</script>

that should work right? somethings not quite right about it though....

Jared
A: 
$('.buttonEffectWrapper').hover(function() {
    $(this).siblings('.rightColumnButtonHighlighted:first').stop().animate({opacity: 1}, {duration:300});
}, function() {
    $(this).siblings('.rightColumnButtonHighlighted:first').stop().animate({opacity: 0}, {duration:300})
})
David
Yours works just as well as the final result I came up with, so you get the vote.
Jared
A: 

Final working code...thanks all!

$(function(){
    $( '.buttonEffectWrapper' ).hover (
        function () {

            var effect = $(this).siblings().filter(":last");
            effect.stop ().animate ( { opacity: 1 }, { duration: 300 } );

        },
        function () {
                    var effect = $(this).siblings().filter(":last");
                  effect.stop ().animate ( { opacity: 0 }, { duration: 300 } );

        }
    );
} );
Jared