How to make the open Toggle close when the second one starts?

When you click on the link with the class splLink, the block element splCont "opens". How to make it so that when you open another splCont, the previously open splCont closes?

<script type="text/javascript"> $(document).ready(function(){ $('.splLink').click(function(){ $(this).parent().children('div.splCont').fadeToggle('fast'); return false; }); }); </script> 
  • @ Acht88, To format a code, select it with the mouse and click on the {} button of the editor. - Nicolas Chabanovsky

2 answers 2

@ KaZac , not really "easy". In your example, the blocks will be hidden, but .fadeToggle () will not work for the current block.

@ Acht88 , I do not know your structure, but I can assume that the link with the class "splLink" and the block with the class "splCont" are related elements. If so, then you can write as follows:

 $('.splLink').click(function(e){ e.preventDefault(); var curBlock = $(this).siblings('.splCont'); $('.splCont').not(curBlock).hide(); curBlock.fadeToggle('fast'); });​ 

You can check the work here .

  • Thank! This is how it works - Acht88

Simply.

 $(document).ready(function(){ $('.splLink').click(function(){ $('div.splCont').hide();//прячем все $(this).parent().children('div.splCont').fadeToggle('fast'); return false; }); }); 
  • Yes thank you. But in this case there is another problem. Now open splCont does not close when you click splLink again. I would appreciate your help with the comment. - Acht88