views:

46

answers:

4

I use the following code to hide and show a box on a bunch of search results. Each result has its own toggle and box to show and hide. The problem I have is that clicking any one of the toggles will show and hide ALL of the boxes on the page and NOT just the box for the one result. How can I do this?

Thanks

jQuery(document).ready(function ($) {

    $("div.MoreResultsTrigger").click(function (e)
    {
        $("div.MoreResultsTrigger").next().slideToggle('fast');
    });

});

UPDATED CODE USING HOVER

 $('a.MoreResultsTrigger').hover(
        function () {
            $(this).next().show();
        },
        function () {
            $(this).next().hide();
        }
    );
+5  A: 

Use the this keyword since you only want to hide the element next to the one you're clicking on.

jQuery(document).ready(function ($) {

    $("div.MoreResultsTrigger").click(function (e)
    {
        $(this).next().slideToggle('fast');
    });

});
Marko
Have and addition question. I have changed the code to use Hover, when also need to box to stay shown if the user hovers the box itself when it appears. Check my original question for the updated code.
Cameron
I'm not quite sure I understand ...
Marko
If a user hovers the Trigger then it will show the next div which is my box. But If the user then hovers the box it will remain shown instead of disappearing because they have removed the hover on the Trigger.
Cameron
+3  A: 

untested but try:

$("div.MoreResultsTrigger").click(function (e)
    {
        $(this).next().slideToggle('fast');
    });
Fermin
+3  A: 

use $(this).next().slideToggle('fast'); that will apply it to ONLY the one you have selected. At the moment its applying the slideToggle to ALL div.MoreResultsTrigger's.

Thomas Clayson
+2  A: 

Try this:

jQuery(document).ready(function ($) {

    $("div.MoreResultsTrigger").click(function()
    {
        $(this).next('.toggleable').slideToggle('fast');
    });

});

The addition of the '.toggleable' class to the .next() is just in case you might have other things in between that you don't want to toggle. You would of course have to add the .toggleable class to all elements that you're interested in toggling. Of course, you can replace, and should, replace toggleable with a name that has more meaning.

Radu
what's toggleable?
Marko