jQuery: next and nextAll
<p>In <a href="http://jsfiddle.net/Jb2jP/15/" rel="nofollow">this</a> code, could you please explain why the first alert shows the correct fieldset's id when the Maintenance button is clicked and the second alert shows the correct fieldset's id when the Subfile maint button is clicked, but not the other way round?</p>

<pre><code>&lt;div&gt;
 &lt;table width="100%"&gt;
  &lt;col width="25%" /&gt;
  &lt;col width="75%" /&gt;
  &lt;tr&gt;
   &lt;td&gt;
    &lt;div class="width300"&gt;&lt;h1&gt;Inandrias Software&amp;#174;&lt;/h1&gt;&lt;/div&gt;
   &lt;/td&gt;
   &lt;td&gt;
    &lt;div class="nopadormarg" style="white-space:nowrap;"&gt;
     &lt;h1&gt;&lt;font size=+1&gt;You are logged in as &lt;/font&gt;charlie&lt;font size=+1&gt; of New Enterprises (Pty) Ltd.&lt;/font&gt;&lt;/h1&gt;
    &lt;/div&gt;
   &lt;/td&gt;
  &lt;/tr&gt;
 &lt;/table&gt;

 &lt;div id="menudiv" class="nopadormarg"&gt;

    &lt;a href="#" class="menuA" name="level01"&gt;&lt;span&gt;Maintenance&lt;/span&gt;&lt;/a&gt;
      &lt;fieldset id="Maintenance" class="menuAbuttons"&gt;
        &lt;p&gt;
          &lt;a href="#" class="menuA" name="level02" title="Maintain ancilliary files"&gt;&lt;span&gt;Subfile maint&lt;/span&gt;&lt;/a&gt;
            &lt;fieldset id="Subfilemaint" class="menuAbuttons"&gt;
              &lt;p&gt;
                &lt;a href="#" class="menuA" name="level03" title="Add, amend, delete (unused) Products"&gt;&lt;span&gt;Products&lt;/span&gt;&lt;/a&gt;
              &lt;/p&gt;
              &lt;p&gt;
                &lt;a href="#" class="menuA" name="level03" title="Add, amend, delete (unused) Insurers"&gt;&lt;span&gt;Insurers&lt;/span&gt;&lt;/a&gt;
              &lt;/p&gt;
              &lt;p&gt;
                &lt;a href="#" class="menuA" name="level03" title="Add, amend, delete (unused) Section types"&gt;&lt;span&gt;Section types&lt;/span&gt;&lt;/a&gt;
              &lt;/p&gt;
              &lt;p&gt;
                &lt;a href="#" class="menuA" name="level03" title="Add, amend, delete (unused) Agents"&gt;&lt;span&gt;Agents&lt;/span&gt;&lt;/a&gt;
              &lt;/p&gt;
              &lt;p&gt;
                &lt;a href="#" class="menuA" name="level03" title="Add, amend, delete (unused) Pay methods"&gt;&lt;span&gt;Pay methods&lt;/span&gt;&lt;/a&gt;
              &lt;/p&gt;
            &lt;/fieldset&gt;
        &lt;/p&gt;
      &lt;/fieldset&gt;

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

<p>​
JS:</p>

<pre><code>$(document).ready(function() {
    $(".menuA").click(function(evnt) {
        evnt.preventDefault();

        alert("next fieldset id = " + $(this).next('fieldset').attr('id'));
        alert("next fieldset id = " + 
$(this).closest('fieldset').find(".menuAbuttons:first").attr('id'));


    // remove the menuA-open class from the current and lower (numerically higher) level's
    //   class menuA objects except the one being clicked (toggled)
    var level = $(this).attr('name');
    // trying to avoid higher levels with &gt;= doesn't seem to work
    //   nor does "[name = " + level + "][name &gt; " + level + "]"
    $(".menuA").not(this).filter("[name = " + level + "]").removeClass("menuA-open");
    //this removes all menuA-open classes except the currently clicked one
    //$(".menuA").not(this).removeClass("menuA-open");

    // hide all other fieldset objects except the one being toggled
    $('fieldset').not($(this).next('fieldset')).hide();

    // toggle the fieldset following this link open or closed
    $("fieldset#" + $(this).text().replace(/\s+/gi,'')).toggle();

    // toggle the currently selected menuA-open class on or off
    $(this).toggleClass("menuA-open");

    });
});
</code></pre>

<p>​</p>
