Search code examples
jqueryeachshowmouseenter

How to select child element in each element with jQuery?


i'm beginer in jQuery. im try in to creat a tooltip. i have something like flowing code:

<div class="view-port">
    <div class="item">
        <a href="#" class="item-link"><img src="product"></a>
        <div class="tooltip">some description</div>
    </div>
    <div class="item">
        <a href="#" class="item-link"><img src="product"></a>
        <div class="tooltip">some description</div>
    </div>
    <div class="item">
        <a href="#" class="item-link"><img src="product"></a>
        <div class="tooltip">some description</div>
    </div>
    <div class="item">
        <a href="#" class="item-link"><img src="product"></a>
        <div class="tooltip">some description</div>
    </div>
</div>

i want to show .tooltip div in each .item element when the mouse enter the each .item element. i wrote the flowing code to do that:

$('.view-port').on('mouseenter', '.item', function(e){             
   $(' .tooltip').show();
});

but when i move the mouse on each item, all the .tooltip shown.

how can i fix that..?!

TnX


Solution

  • You should try this (.find() will search the element in the current element selected with $(this)):

    $('.view-port').on('mouseenter', '.item', function(e){             
       $(this).find('.tooltip').show();
    });
    

    or this:

    $('.view-port').on('mouseenter', '.item', function(e){             
       $('.tooltip', this).show()
    });