tags:

views:

30

answers:

3

Hi,

I have a list of divs (simplified example)

<div class="title">text</div>
<div class="description">text</div>
<div class="title">text</div>
<div class="description">text</div>
<div class="title">text</div>
<div class="description">text</div>

.description is hidden on page load when I hover over a title i show the description to that title, when I hover over another title I show the description to that title, but what I also want to do is to hide that last .description that was open.

I tried with mouseout, mouseleave etc. but didn't get it to work. Any suggestions?

    $('.title').mouseover(function () {
            $(this).next().fadeIn('fast');
        });

        $('.title')..mouseleave(function () {
            $(this).next().slideOut('fast');
        });

With this I hide the description if I hover over the same title again but not other titles.

    jQuery.fn.fadeToggle = function (speed, easing, callback) {
        return this.animate({ opacity: 'toggle' }, speed, easing, callback);
    };

    $('.title').mouseover(function () {
            $(this).next().fadeToggle('fast');
    });
A: 

You can specify their class selector to hide them all, in this way only one of them will be shown at a time:

   $('.title').mouseover(function () {
     $('.description').hide();
     $(this).next().fadeIn('fast');
   });
Sarfraz
ahh too easy, thanks (have to wait a couple of min to accept solution)
martin
once I have started to show any description there will always be one shown, is there a way to hide all descriptions if I don't have had the mouse over a .title or .description for some time?
martin
@martin: You can use the mouseleave method similarly and hide all description elements like I showed :)
Sarfraz
yeah I realized that, I have a link at the bottom of the description divs though so if I leave .title to click on the link the div becomes hidden which isn't very good. Sorry for not posting this information in my original question.
martin
@martin: No problem :)
Sarfraz
ok so I wrap it in an additional div and add mouseleave to that one.
martin
A: 

You've made a typo here:

$('.title')..mouseleave(function () {

There's two periods.

Hope it helps.

Ian
A: 

Your code works if you use the right function. There is no slideOut method. Use either fadeOut() or slideUp(), e.g.:

$('.title').mouseover(function () {
    $(this).next().fadeIn('fast');
});

$('.title').mouseleave(function () {
    $(this).next().fadeOut('fast');
});

See: http://jsfiddle.net/Tjcvz/

Felix Kling
thanks for the comment, I didn't see your reply until after I tried Sarfraz solution.
martin