Josh Boothe
Josh Boothe

Reputation: 1423

Selecting an element in the same parent as target

Assuming I have a set up of:

<ul id="cal">
    <li><span>test</span><img /></li>
    <li><span>test</span><img /></li>
    <li><span>test</span><img /></li>
</ul>

and in the jQuery:

$('#cal li span').hover(function(){
    $("#cal li img").trigger('mouseover');
});

I need to trigger mouseover on the #cal li img, but on only the image of the span rather than all of the images

Upvotes: 0

Views: 80

Answers (3)

karaxuna
karaxuna

Reputation: 26940

$('#cal li span').hover(function(){
    $(this).next("img").trigger('mouseover');
});

Upvotes: 0

Samuel Caillerie
Samuel Caillerie

Reputation: 8275

Specify it is linked to your context with:

$('#cal li span').hover(function(){
    $(this).parent().find("img").trigger('mouseover');
});

Upvotes: 1

VisioN
VisioN

Reputation: 145458

You can refer to closest <img> using $(this).next() or with:

$("#cal li span").hover(function() {
    $(this).siblings("img").trigger("mouseover");
});

Upvotes: 3

Related Questions