views:

55

answers:

1
$(function() {
    $("table.section thead").click(function() {
      if ($(this).next("table.section tbody").style.display == "block"){
         $(this).next("table.section tbody").slideUp("slow");
      }
      if ($(this).next("table.section tbody").style.display == "none"){
         $(this).next("table.section tbody").slideDown("slow");
      }
    });
});

I dont know how to achieve this, any help would be very much appreciated.

UPDATE:

I was trying to use the following function.

$(function() {
    $("table.section thead").click(function() {
    $(this).next("table.section tbody").slideToggle("slow");

    });
});

Which it giving me a problem, (when thead it collapse and you click on it, it expands and then collapse again, so it would always be collapse). That is why I am trying to get the function at the top.

+5  A: 

Use the visible selector in this case:

$(function() {
    $("table.section thead").click(function() {
      var body = $(this).next("table.section tbody");
      if (body.is(":visible"))
         body.slideUp("slow");
      else
         body.slideDown("slow");
    });
});

But probably much simpler just use .slideToggle() like this:

$(function() {
    $("table.section thead").click(function() {
      $(this).next("table.section tbody").slideToggle("slow");
    });
});
Nick Craver
Agreed on `slideToggle()`, this makes things **a lot** simpler.
Ben Poole
@Nick: Thanks, that is What I was looking for, but the problem I am getting with the simpler function, stills happend, when the tbody its not visible it expands and collapses with the same click, and I tried to use return false, Do you know why this may be? Thanks.
Cesar Lopez
I added an alert in the if statement and another on else, and it always gets to the if so it does slideUP all the time.
Cesar Lopez
@Cesar - Sounds like you're attaching the event twice, are you running or including the function 2 times?
Nick Craver
its a very big form, but I cant not see the function or anything similar any other place.
Cesar Lopez
If the tbody only have one row it works, but if it has more than one it does that strange thing. Any Ideas?
Cesar Lopez