Mirgorod
Mirgorod

Reputation: 32653

Jquery hover only at the last element

I have following html structure:

<div id="123" class="test">
   <div class="testMessage">Foo</div>
   <div><div class="testDate">2010</div></div>
   <div id="127" class="test">
      <div class="testMessage">Bar</div>
      <div><div class="testDate">2011</div></div>
   </div>
</div>

And following JS code:

$(".test").live({
    mouseenter:
        function()
        {
            $(this).find(".testDate").show();
        },
    mouseleave:
        function()
        {
            $(this).find(".testDate").hide();
        }
});

The problem is that when mouse pointer is at #127 .testDate in #123 also displayed. I think it's because hover works for parent element. How to fix it?

Thanx!

Upvotes: 0

Views: 349

Answers (2)

thecodeparadox
thecodeparadox

Reputation: 87073

$(".test").live({
    mouseenter: function() {
        $('.testDate:first', this).show();
    },
    mouseleave: function() {
        $('.testDate:first', this).hide();
    }
});

Upvotes: 2

servik
servik

Reputation: 3101

I think this is because you have one element '.test' inside another '.test'. If you split them, your code will work. Here is working example.

Upvotes: 2

Related Questions