views:

183

answers:

2

I have the following markup:

<div class="header">Text</div>
<div class="hiddenArea">sdsada</div>

<div class="header">Text2</div>
<div class="hiddenArea">sdsada</div>

and the following jQuery:

$('.header').click(function() {
        var t = $(this).next('.hiddenArea').slideToggle();
    });

When the hiddenArea is revealed I want to hide the other hiddenArea if it is visible? I want to make this so that I can add other headers and hiddenAreas if I need them.

Update:

Thanks Guys, ended up doing this:

$('#messages .header').click(function() {
  if (!$(this).next().is(':visible')) {
    $('.hiddenArea').slideToggle();
  }
});
+3  A: 

Assuming you have one hiddenArea visible when the form is rendered this will work. Also note you dont need the filter inside the next method as next only ever gives you the next sibling.

$('.header').click(function() {
    var $el = $(this);
    if ( ! $el.next().is('visible') ){
       $('div.hiddenArea:visible').slideUp( function(){
           var t = $el.next().slideDown();
       });
    }
});
redsquare
this works great redsquare thanks, but both hiddenAreas are not visible initially!?
Rigobert Song
it only needs one to be visible initially!
redsquare
changed the code to make it work correctly..
redsquare
just made the first one visible, thanks!
Rigobert Song
when you click on the header where the next hiddenArea is already visible, the hiddenArea is hidden then revealed, not very slick, i was hoping to do nothing if the appropriate hiddenArea was already visible
Rigobert Song
thats a late breaking change to requirements!! will edit the above
redsquare
A: 

Thanks Guys, ended up doing this:

$('#messages .header').click(function() {
        if (!$(this).next().is(':visible')) {
            $('.hiddenArea').slideToggle();
        }
    });
Rigobert Song
I never trust any of the toggle's as they can get out of synch with quick double clicks etc. Anyway cool you got it sorted
redsquare