views:

345

answers:

2

I want only one drawer to be open at startup. At the moment, all drawers are open.

The jQuery code

$(document).ready(function () {
    // hide all ULs inside LI.drawer except the first one
    $('LI.drawer UL:not(:first)').hide();

    // apply the open class
    $('li.drawer:first ul').addClass('open');

    $('h2.drawer-handle').click(function () {
        // hide the currently visible drawer contents
        $('li.drawer ul:visible').hide();

        // remove the open class from the currently open drawer
        $('h2.open').removeClass('open');

        // show the associated drawer content to 'this' (this is the current H2 element)
        // since the drawer content is the next element after the clicked H2, we find
        // it and show it using this:
        $(this).next().show();

        // set a class indicating on the H2 that the drawer is open
        $(this).addClass('open');
    });
});

The HTML in the body

<ul class="drawers">

   <li class="drawer">
      <h2 class="drawer-handle open">Contact</h2>
      <ul>
           <li>A</li>
           <li>B</li>
      </ul>
   </li>

   <li class="drawer">
      <h2 class="drawer-handle">papers</h2>
      <ul>
           <li>A</li>
           <li>B</li>
      </ul>
   </li>

</ul>

How can you show one drawer and hide the rest at the startup?

+1  A: 

You've got the right idea, just the wrong placement of your :first clause.

$('li.drawer:first ul').addClass('open');
Elliot Nelson
Thank you for pointing that out!
Masi
The same problem seems to occur still.
Masi
I think the same problem still exists, but in the hide() line. Your current first line says "within every LI.drawer, hide all ULs that are not the first one", which won't hide anything. I think you want 'LI.drawer:not(:first) UL'.
Elliot Nelson
Your code works! Thank you!
Masi
A: 

The original code works too.

I had the following bug in my code at the server

$(document).ready(function () {
// hide all ULs inside LI.drawer except the first one $('LI.drawer UL:not(:first)').hide();

...

The first line of my code was under the comment mark.

Masi