self-made accordion should fold open menu items before opening a new one
<p>I'm learning jQuery right now and the first "bigger" project is a self made Accordion/sliding Navigation. </p>

<p>My HTML Code looks like this:</p>

<pre><code>&lt;dl&gt;
 &lt;dt&gt;&lt;a href="#"&gt;Prologue&lt;/a&gt;&lt;/dt&gt;
&lt;dd id="prolog"&gt;
 &lt;div&gt;TEST&lt;/div&gt;
&lt;/dd&gt;

&lt;dt&gt;&lt;a href="#"&gt;Projekte&lt;/a&gt;&lt;/dt&gt;
&lt;dd id="projekte"&gt;
 &lt;ul&gt;
     &lt;li&gt;TEST&lt;/li&gt;
        &lt;li&gt;TEST&lt;/li&gt;
        &lt;li&gt;TEST&lt;/li&gt;
        &lt;li&gt;TEST&lt;/li&gt;
&lt;/ul&gt;
&lt;/dd&gt;

&lt;dt&gt;&lt;a href="#"&gt;Fotografie&lt;/a&gt;&lt;/dt&gt;
&lt;dd id="fotos"&gt;
 &lt;ul&gt;
     &lt;li&gt;TEST&lt;/li&gt;
        &lt;li&gt;TEST&lt;/li&gt;
        &lt;li&gt;TEST&lt;/li&gt;
        &lt;li&gt;TEST&lt;/li&gt;
&lt;/ul&gt;
&lt;/dd&gt;

&lt;dt&gt;&lt;a href="#"&gt;Analog&lt;/a&gt;&lt;/dt&gt;
&lt;dd id="analog"&gt;
 &lt;ul&gt;
     &lt;li&gt;TEST&lt;/li&gt;
        &lt;li&gt;TEST&lt;/li&gt;
        &lt;li&gt;TEST&lt;/li&gt;
        &lt;li&gt;TEST&lt;/li&gt;
&lt;/ul&gt;
&lt;/dd&gt;

&lt;dt&gt;&lt;a href="#"&gt;Über mich&lt;/a&gt;&lt;/dt&gt;
&lt;dd id="about"&gt;
 &lt;ul&gt;
     &lt;li&gt;TEST&lt;/li&gt;
        &lt;li&gt;TEST&lt;/li&gt;
        &lt;li&gt;TEST&lt;/li&gt;
        &lt;li&gt;TEST&lt;/li&gt;
&lt;/ul&gt;
&lt;/dd&gt;

&lt;dt&gt;&lt;a href="#"&gt;Kontakt&lt;/a&gt;&lt;/dt&gt;
&lt;dd id="kontakt"&gt;
 &lt;ul&gt;
     &lt;li&gt;TEST&lt;/li&gt;
        &lt;li&gt;TEST&lt;/li&gt;
        &lt;li&gt;TEST&lt;/li&gt;
        &lt;li&gt;TEST&lt;/li&gt;
&lt;/ul&gt;
&lt;/dd&gt;
</code></pre>

<p></p>

<p>And here comes the jQuery code. The idea is to check if the user clicks on an open accordion item and if he does, just close it. Otherwise (when the clicked item is closed) it checks, if any other of the accordion items is open. If they are all hidden, it opens the clicked item. Otherwise it closes all items (don't know how to get only the open one) and opens the clicked item. This is what it looks like: </p>

<pre><code>$("dt a").click(function(){

var checkElement = $(this).parent("dt").next("dd");

var main1 = $('#prolog'); var main2 = $('#projekte'); var main3 = $('#fotos'); var main4 = $('#analog'); var main5 = $('#about'); var main6 = $('#kontakt');

if(checkElement.is(':visible')) {
    $(this).parent("dt").next("dd").slideUp("normal"); 
    return false
} else {
    if( (main1.is(':hidden')) &amp;&amp;
        (main2.is(':hidden')) &amp;&amp;    
        (main3.is(':hidden')) &amp;&amp;
        (main4.is(':hidden')) &amp;&amp;
        (main5.is(':hidden')) &amp;&amp;
        (main6.is(':hidden')) ) {$(this).parent("dt").next("dd").slideDown("normal");}
    else {
        $("dd").slideUp("normal"), checkElement.slideDown("normal");
    }

    return false;
};

});
</code></pre>

<p>So far it works fine (though the second if-request with the bunch of variables doesn't look very professional, if you could help me out here, it would be nice, although it's not the main issue). Next goal is to put the closing and opening of the accordion items in a row. I want the slideUp first and after it's finished, the slideDown should start. So I changed</p>

<pre><code>else {
        $("dd").slideUp("normal"), checkElement.slideDown("normal");
    }
</code></pre>

<p>to</p>

<pre><code>else {
   $("dd").slideUp("normal", function() { checkElement.slideDown('normal') });
}
</code></pre>

<p>But here comes the bug: It first closes + opens at the same time and after that, it does the same but in a row (like I want it to). Seems like it does this part of the function twice, but one time simultaneous and afterward in a row. I just can't figure out, where the first, simultaneous animation comes from. Especially because if the functions aren't in a row, it works just fine (without doing the same twice).</p>

<p>Would be nice, if anyone could help me with this.</p>

<p>Regards<br>
Lucas</p>

<p>PS: And please don't send me over to a finished Accordion (plugin), I want to learn and understand creating one!</p>

<p>PS2: Sorry for my poor English, I'm from Germany. ;-)</p>

<p>PS3: I leave for holidays later on, but I'll be back in 10 days!</p>
