Hiding elements based on last closed element jquery script
<p>how can I make this jquery script close all previously opened children when entering a new parent? At the moment it traverses thru all the tree structure fine, but switching from one parent to another does not close the previous children, but rather only the each individual parents elements as a user browses.</p>

<p>Here is the jquery I'm using:</p>

<pre><code>&lt;script type="text/javascript"&gt;
    $(document).ready($(function(){
      $('#nav&gt;li&gt;ul').hide();
      $('.children').hide();

      $('#nav&gt;li').mousedown(function(){
        // check that the menu is not currently animated
        if ($('#nav ul:animated').size() == 0) {
          // create a reference to the active element (this)
          // so we don't have to keep creating a jQuery object
          $heading = $(this);
          // create a reference to visible sibling elements
          // so we don't have to keep creating a jQuery object
          $expandedSiblings = $heading.siblings().find('ul:visible');
          if ($expandedSiblings.size() &gt; 0) {
            $expandedSiblings.slideUp(0, function(){
                $heading.find('ul').slideDown(0);
            });
          }
          else {
            $heading.find('ul').slideDown(0);
          }
        }
      });

      $('#nav&gt;li&gt;ul&gt;li').mousedown(function(){
        // check that the menu is not currently animated
        if ($('#nav ul:animated').size() == 0) {
          // create a reference to the active element (this)
          // so we don't have to keep creating a jQuery object
          $heading2 = $(this);
          // create a reference to visible sibling elements
          // so we don't have to keep creating a jQuery object
          $expandedSiblings2 = $heading2.siblings().find('.children:visible');
          if ($expandedSiblings2.size() &gt; 0) {
            $expandedSiblings2.slideUp(0, function(){
                $heading2.find('.children').slideDown(0);
            });
          } else {
            $heading2.find('.children').slideDown(0);
          }
        }    

       });

    }));

    &lt;/script&gt;
</code></pre>

<p>and here is my html output</p>

<pre><code>&lt;ul id="nav"&gt; 

&lt;li&gt;&lt;a href="#"&gt;folder 4&lt;/a&gt;

&lt;ul&gt;&lt;li&gt;&lt;a href="#"&gt;2001&lt;/a&gt;

&lt;ul&gt;&lt;li class="children"&gt;&lt;a href="./directory//folder 4/2001/doc1.txt"&gt;doc1.txt&lt;/a&gt;&lt;/li&gt;

&lt;li class="children"&gt;&lt;a href="./directory//folder 4/2001/doc2.txt"&gt;doc2.txt&lt;/a&gt;&lt;/li&gt;

&lt;li class="children"&gt;&lt;a href="./directory//folder 4/2001/doc3.txt"&gt;doc3.txt&lt;/a&gt;&lt;/li&gt;

&lt;/ul&gt;

&lt;/li&gt;

&lt;li&gt;&lt;a href="#"&gt;2002&lt;/a&gt;

&lt;ul&gt;&lt;li class="children"&gt;&lt;a href="./directory//folder 4/2002/doc1.txt"&gt;doc1.txt&lt;/a&gt;&lt;/li&gt;

&lt;li class="children"&gt;&lt;a href="./directory//folder 4/2002/doc2.txt"&gt;doc2.txt&lt;/a&gt;&lt;/li&gt;

&lt;li class="children"&gt;&lt;a href="./directory//folder 4/2002/doc3.txt"&gt;doc3.txt&lt;/a&gt;&lt;/li&gt;

&lt;li class="children"&gt;&lt;a href="./directory//folder 4/2002/doc4.txt"&gt;doc4.txt&lt;/a&gt;&lt;/li&gt;

&lt;/ul&gt;

&lt;/li&gt;

&lt;li&gt;&lt;a href="#"&gt;2003&lt;/a&gt;

&lt;ul&gt;&lt;li class="children"&gt;&lt;a href="./directory//folder 4/2003/Copy of doc1.txt"&gt;Copy of doc1.txt&lt;/a&gt;&lt;/li&gt;

&lt;li class="children"&gt;&lt;a href="./directory//folder 4/2003/doc1.txt"&gt;doc1.txt&lt;/a&gt;&lt;/li&gt;

&lt;li class="children"&gt;&lt;a href="./directory//folder 4/2003/doc2.txt"&gt;doc2.txt&lt;/a&gt;&lt;/li&gt;

&lt;/ul&gt;

&lt;/li&gt;

&lt;li&gt;&lt;a href="#"&gt;2004&lt;/a&gt;

&lt;ul&gt;&lt;li class="children"&gt;&lt;a href="./directory//folder 4/2004/doc1.txt"&gt;doc1.txt&lt;/a&gt;&lt;/li&gt;

&lt;li class="children"&gt;&lt;a href="./directory//folder 4/2004/doc2.txt"&gt;doc2.txt&lt;/a&gt;&lt;/li&gt;

&lt;li class="children"&gt;&lt;a href="./directory//folder 4/2004/doc3.txt"&gt;doc3.txt&lt;/a&gt;&lt;/li&gt;

&lt;li class="children"&gt;&lt;a href="./directory//folder 4/2004/doc4.txt"&gt;doc4.txt&lt;/a&gt;&lt;/li&gt;

&lt;/ul&gt;

&lt;/li&gt;

&lt;/ul&gt;

&lt;/li&gt;

&lt;li&gt;&lt;a href="#"&gt;folder1&lt;/a&gt;

&lt;ul&gt;&lt;li&gt;&lt;a href="#"&gt;2001&lt;/a&gt;

&lt;ul&gt;&lt;li class="children"&gt;&lt;a href="./directory//folder1/2001/doc1.txt"&gt;doc1.txt&lt;/a&gt;&lt;/li&gt;

&lt;li class="children"&gt;&lt;a href="./directory//folder1/2001/doc2.txt"&gt;doc2.txt&lt;/a&gt;&lt;/li&gt;

&lt;li class="children"&gt;&lt;a href="./directory//folder1/2001/doc3.txt"&gt;doc3.txt&lt;/a&gt;&lt;/li&gt;

&lt;/ul&gt;

&lt;/li&gt;

&lt;li&gt;&lt;a href="#"&gt;2002&lt;/a&gt;

&lt;ul&gt;&lt;li class="children"&gt;&lt;a href="./directory//folder1/2002/doc1.txt"&gt;doc1.txt&lt;/a&gt;&lt;/li&gt;

&lt;li class="children"&gt;&lt;a href="./directory//folder1/2002/doc2.txt"&gt;doc2.txt&lt;/a&gt;&lt;/li&gt;

&lt;li class="children"&gt;&lt;a href="./directory//folder1/2002/doc3.txt"&gt;doc3.txt&lt;/a&gt;&lt;/li&gt;

&lt;li class="children"&gt;&lt;a href="./directory//folder1/2002/doc4.txt"&gt;doc4.txt&lt;/a&gt;&lt;/li&gt;

&lt;/ul&gt;

&lt;/li&gt;

&lt;li&gt;&lt;a href="#"&gt;2003&lt;/a&gt;

&lt;ul&gt;&lt;li class="children"&gt;&lt;a href="./directory//folder1/2003/Copy of doc1.txt"&gt;Copy of doc1.txt&lt;/a&gt;&lt;/li&gt;

&lt;li class="children"&gt;&lt;a href="./directory//folder1/2003/doc1.txt"&gt;doc1.txt&lt;/a&gt;&lt;/li&gt;

&lt;li class="children"&gt;&lt;a href="./directory//folder1/2003/doc2.txt"&gt;doc2.txt&lt;/a&gt;&lt;/li&gt;

&lt;/ul&gt;

&lt;/li&gt;

&lt;li&gt;&lt;a href="#"&gt;2004&lt;/a&gt;

&lt;ul&gt;&lt;li class="children"&gt;&lt;a href="./directory//folder1/2004/doc1.txt"&gt;doc1.txt&lt;/a&gt;&lt;/li&gt;

&lt;li class="children"&gt;&lt;a href="./directory//folder1/2004/doc2.txt"&gt;doc2.txt&lt;/a&gt;&lt;/li&gt;

&lt;li class="children"&gt;&lt;a href="./directory//folder1/2004/doc3.txt"&gt;doc3.txt&lt;/a&gt;&lt;/li&gt;

&lt;li class="children"&gt;&lt;a href="./directory//folder1/2004/doc4.txt"&gt;doc4.txt&lt;/a&gt;&lt;/li&gt;

&lt;/ul&gt;

&lt;/li&gt;

&lt;/ul&gt;

&lt;/li&gt;

&lt;li&gt;&lt;a href="#"&gt;folder2&lt;/a&gt;

&lt;ul&gt;&lt;li&gt;&lt;a href="#"&gt;2001&lt;/a&gt;

&lt;ul&gt;&lt;li class="children"&gt;&lt;a href="./directory//folder2/2001/doc1.txt"&gt;doc1.txt&lt;/a&gt;&lt;/li&gt;

&lt;li class="children"&gt;&lt;a href="./directory//folder2/2001/doc2.txt"&gt;doc2.txt&lt;/a&gt;&lt;/li&gt;

&lt;li class="children"&gt;&lt;a href="./directory//folder2/2001/doc3.txt"&gt;doc3.txt&lt;/a&gt;&lt;/li&gt;

&lt;/ul&gt;

&lt;/li&gt;

&lt;li&gt;&lt;a href="#"&gt;2002&lt;/a&gt;

&lt;ul&gt;&lt;li class="children"&gt;&lt;a href="./directory//folder2/2002/doc1.txt"&gt;doc1.txt&lt;/a&gt;&lt;/li&gt;

&lt;li class="children"&gt;&lt;a href="./directory//folder2/2002/doc2.txt"&gt;doc2.txt&lt;/a&gt;&lt;/li&gt;

&lt;li class="children"&gt;&lt;a href="./directory//folder2/2002/doc3.txt"&gt;doc3.txt&lt;/a&gt;&lt;/li&gt;

&lt;li class="children"&gt;&lt;a href="./directory//folder2/2002/doc4.txt"&gt;doc4.txt&lt;/a&gt;&lt;/li&gt;

&lt;/ul&gt;

&lt;/li&gt;

&lt;li&gt;&lt;a href="#"&gt;2003&lt;/a&gt;

&lt;ul&gt;&lt;li class="children"&gt;&lt;a href="./directory//folder2/2003/Copy of doc1.txt"&gt;Copy of doc1.txt&lt;/a&gt;&lt;/li&gt;

&lt;li class="children"&gt;&lt;a href="./directory//folder2/2003/doc1.txt"&gt;doc1.txt&lt;/a&gt;&lt;/li&gt;

&lt;li class="children"&gt;&lt;a href="./directory//folder2/2003/doc2.txt"&gt;doc2.txt&lt;/a&gt;&lt;/li&gt;

&lt;/ul&gt;

&lt;/li&gt;

&lt;li&gt;&lt;a href="#"&gt;2004&lt;/a&gt;

&lt;ul&gt;&lt;li class="children"&gt;&lt;a href="./directory//folder2/2004/doc1.txt"&gt;doc1.txt&lt;/a&gt;&lt;/li&gt;

&lt;li class="children"&gt;&lt;a href="./directory//folder2/2004/doc2.txt"&gt;doc2.txt&lt;/a&gt;&lt;/li&gt;

&lt;li class="children"&gt;&lt;a href="./directory//folder2/2004/doc3.txt"&gt;doc3.txt&lt;/a&gt;&lt;/li&gt;

&lt;li class="children"&gt;&lt;a href="./directory//folder2/2004/doc4.txt"&gt;doc4.txt&lt;/a&gt;&lt;/li&gt;

&lt;/ul&gt;

&lt;/li&gt;

&lt;/ul&gt;

&lt;/li&gt;

&lt;li&gt;&lt;a href="#"&gt;folder3&lt;/a&gt;

&lt;ul&gt;&lt;li&gt;&lt;a href="#"&gt;2001&lt;/a&gt;

&lt;ul&gt;&lt;li class="children"&gt;&lt;a href="./directory//folder3/2001/doc1.txt"&gt;doc1.txt&lt;/a&gt;&lt;/li&gt;

&lt;li class="children"&gt;&lt;a href="./directory//folder3/2001/doc2.txt"&gt;doc2.txt&lt;/a&gt;&lt;/li&gt;

&lt;li class="children"&gt;&lt;a href="./directory//folder3/2001/doc3.txt"&gt;doc3.txt&lt;/a&gt;&lt;/li&gt;

&lt;/ul&gt;

&lt;/li&gt;

&lt;li&gt;&lt;a href="#"&gt;2002&lt;/a&gt;

&lt;ul&gt;&lt;li class="children"&gt;&lt;a href="./directory//folder3/2002/doc1.txt"&gt;doc1.txt&lt;/a&gt;&lt;/li&gt;

&lt;li class="children"&gt;&lt;a href="./directory//folder3/2002/doc2.txt"&gt;doc2.txt&lt;/a&gt;&lt;/li&gt;

&lt;li class="children"&gt;&lt;a href="./directory//folder3/2002/doc3.txt"&gt;doc3.txt&lt;/a&gt;&lt;/li&gt;

&lt;li class="children"&gt;&lt;a href="./directory//folder3/2002/doc4.txt"&gt;doc4.txt&lt;/a&gt;&lt;/li&gt;

&lt;/ul&gt;

&lt;/li&gt;

&lt;li&gt;&lt;a href="#"&gt;2003&lt;/a&gt;

&lt;ul&gt;&lt;li class="children"&gt;&lt;a href="./directory//folder3/2003/Copy of doc1.txt"&gt;Copy of doc1.txt&lt;/a&gt;&lt;/li&gt;

&lt;li class="children"&gt;&lt;a href="./directory//folder3/2003/doc1.txt"&gt;doc1.txt&lt;/a&gt;&lt;/li&gt;

&lt;li class="children"&gt;&lt;a href="./directory//folder3/2003/doc2.txt"&gt;doc2.txt&lt;/a&gt;&lt;/li&gt;

&lt;/ul&gt;

&lt;/li&gt;

&lt;li&gt;&lt;a href="#"&gt;2004&lt;/a&gt;

&lt;ul&gt;&lt;li class="children"&gt;&lt;a href="./directory//folder3/2004/doc1.txt"&gt;doc1.txt&lt;/a&gt;&lt;/li&gt;

&lt;li class="children"&gt;&lt;a href="./directory//folder3/2004/doc2.txt"&gt;doc2.txt&lt;/a&gt;&lt;/li&gt;

&lt;li class="children"&gt;&lt;a href="./directory//folder3/2004/doc3.txt"&gt;doc3.txt&lt;/a&gt;&lt;/li&gt;

&lt;li class="children"&gt;&lt;a href="./directory//folder3/2004/doc4.txt"&gt;doc4.txt&lt;/a&gt;&lt;/li&gt;

&lt;/ul&gt;

&lt;/li&gt;

&lt;/ul&gt;

&lt;/li&gt;

&lt;/ul&gt;
</code></pre>

<p>I assume my problem is, jquery isn't closing the children between each new parent so I need to make a call, but I'm a bit lost on how to do that.</p>

<p>I know the code is pretty messy, this project was done in a huge rush and a very tight timeframe. </p>

<p>Appreciate your answers and any other constructive comments, cheers :)</p>
