JQuery Slide Menu Slide Toggle on click

Hi,

I have this jquery slide menu code that I am having problems getting the menu to close on the second click.  So that if a menu is open and you click it again it will close.  AS it is right now it  will not close it just shows the menu again.  

I would also like for an image to open and close based on if the menu is open or closed.  I am new to jquery and I am having problems figuring this out.  

Thanks for the help.  

Kevin


Here is the code:  
    
    <TITLE>Slide Demo - Fixed Drawer</TITLE>
    <style type="text/css">
    BODY { margin: 10px; padding: 0; font-family: "Helvetica Neue', Helvetica, Arial, Sans-Serif";}
H2 { font-size: 20px;}
    UL.drawers {
        width: 317px;
       padding-bottom: 10px;
    }
    
    UL {
        list-style: none;
        margin: 0;
        padding: 5px;
    }
    
    LI.drawerssub A {
        text-decoration: none;
        color: #000000;
        list-style: none;
        
    }
    
       LI.drawerssub A:HOVER {
        text-decoration: none;
        color: #E94C24;
        
    }
    
    
    H2.drawer-handle {
        margin: 0;
        padding: 10px;
        background-color: #BAB1AD;
        height: 28px;
        margin-bottom: 3px;
        list-style: none;
    }
    
    H2.drawer-handle.open {
        background-color: #BAB1AD;
        list-style: none;
    }
        
    -->
    </STYLE>

    <SCRIPT src="./Slide Demo - Fixed Drawer_files/jquery-1.2.1.js" type="text/javascript"></SCRIPT>

    <SCRIPT type="text/javascript">
    <!--    
    $(document).ready(function () {
      $('LI.drawer UL').hide(); // hide all ULs inside LI.drawer except the first one
      $('H2.drawer-handle').click(function () {
        // hide all the drawer contents
        $('LI.drawer UL:visible').slideUp().prev().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).addClass('open').next().slideDown();
      });
    });
    //-->    </SCRIPT>
</HEAD><BODY id="page">
    
    <DIV class="drawers-wrapper">
	<DIV class="boxcap captop"></DIV>
    <UL class="drawers" id="nav">
        <LI class="drawer" style="position: static; ">
            <H2 class="drawer-handle open"><IMG src="RightArrow.gif" />     WORSHIP</H2>
            <UL class="alldownloads" style="display: block; ">
         <li class="drawerssub"><a href="#">Sunday Worship</a> </li>
        <li class="drawerssub"><a href="#">Childcare</a> </li>
        <li class="drawerssub"><a href="#">Current Sermon Series</a> </li>
        <li class="drawerssub"><a href="#">Audio & Study Guides</a> </li>
        <li class="drawerssub"><a href="#">Music & Choirs</a> </li>
        <li class="drawerssub"><a href="#">Worship at Union</a> </li>
            </UL>
        </LI>
        <LI class="drawer">
            <H2 class="drawer-handle"><IMG src="RightArrow.gif" />     CONNECT TO COMMUNITY</H2>
            <UL style="display: none; ">
 
        <li class="drawerssub"><a href="#">Classes</a> </li>
        <li class="drawerssub"><a href="#">Events</a> </li>
        <li class="drawerssub"><a href="#">Baptism</a> </li>
        <li class="drawerssub"><a href="#">Membership</a> </li>
        <li class="drawerssub"><a href="#">Caring Ministries and Resources</a> </li>
            </UL>
        </LI>
        <LI class="drawer">
            <H2 class="drawer-handle"><IMG src="RightArrow.gif" />     SERVE</H2>
            <UL style="display: none; ">
        <li class="drawerssub"><a href="#">Serve around UPC</a> </li>
        <li class="drawerssub"><a href="#">Serve around Seattle</a> </li>
        <li class="drawerssub"><a href="#">Serve around the World</a> </li>
            </ul>
            </LI>
         <LI class="drawer">
            <H2 class="drawer-handle"><IMG src="RightArrow.gif" />     SMALL GROUPS</H2>
            <UL style="display: none; ">
        <li class="drawerssub"><a href="#">Join a Group</a> </li>
        <li class="drawerssub"><a href="#">For Group Facilitators</a> </li>
        <li class="drawerssub"><a href="#">Sermon Study Guides & Audio</a> </li>
        <li class="drawerssub"><a href="#">Ideas for Serving Together</a> </li>
            </ul>
            </li>
            <LI class="drawer">
            <H2 class="drawer-handle"><IMG src="RightArrow.gif" />     MINISTRIES</H2>
            <UL style="display: none; ">
        <li class="drawerssub"><a href="#">Children & Families</a> </li>
        <li class="drawerssub"><a href="#">Youth Mission & Ministry</a> </li>
        <li class="drawerssub"><a href="#">University</a> </li>
        <li class="drawerssub"><a href="#">20/30s</a> </li>
        <li class="drawerssub"><a href="#">Adults</a> </li>
        <li class="drawerssub"><a href="#">ESL & Internationals</a> </li>
        <li class="drawerssub"><a href="#">Side-by-Side</a> </li>
        <li class="drawerssub"><a href="#">Hospitality</a> </li>

            </UL>
        </LI>
    </UL>
	<DIV class="boxcap"></DIV>
	</DIV>

</BODY></HTML>

Open in new window

LVL 1
upchelpdeskAsked:
Who is Participating?
 
StealthyDevConnect With a Mentor Commented:
And I would like to give you the script for increasing/decreasing the height as per your requirement.

Please note that your font size is 20px and you also have some padding, so shrinking to 18px is not possible. I have given an example of having shrink-text in 45px and expanded text in 55px..

Regards.
$(document).ready(function () {
	$('LI.drawer UL').hide(); // hide all ULs inside LI.drawer except the first one
	$('H2.drawer-handle').click(function () {
		$('LI.drawer UL:visible').slideUp().prev().removeClass('open').addClass("MarkClosed");
		$(this).not(".MarkClosed").addClass('open').next().slideDown();
		$('H2.drawer-handle').find('IMG').attr("src","Aiga_rightarrow.gif");
		$(this).not(".MarkClosed").find('IMG').attr("src","Aiga_leftarrow.gif");
		$('H2.drawer-handle').css("height", "45px");
		$(this).not(".MarkClosed").css("height", "55px");
		$(".MarkClosed").removeClass("MarkClosed");
	});
});

Open in new window

0
 
jayantcCommented:
Here is an example:

http://jqueryui.com/demos/accordion/#collapsible

- JayantC
0
 
StealthyDevCommented:
Hi, you can use this:

      $(document).ready(function () {
            $('LI.drawer UL').hide(); // hide all ULs inside LI.drawer except the first one
            $('H2.drawer-handle').click(function () {
                  $('LI.drawer UL:visible').slideUp().prev().removeClass('open').addClass("MarkClosed");
                  $(this).not(".MarkClosed").addClass('open').next().slideDown();
                  $(".MarkClosed").removeClass("MarkClosed");
            });
      });

Also, if you want to show some other Image, where is the image, and when to show?

Regards.
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
upchelpdeskAuthor Commented:
senthurpandian,

Thanks for the reply, the image I want to show is found in this line:  

<H2 class="drawer-handle open"><IMG src="RightArrow.gif" />  

If Open I would change it to like <H2 class="drawer-handle open"><IMG src="DownArrow.gif" />

Make sense?  
0
 
tushar-mahajanCommented:
Hi just add few lines in your code
 
$(document).ready(function () {
            $('LI.drawer UL').hide(); // hide all ULs inside LI.drawer except the first one
            $('H2.drawer-handle').click(function () {
                // your code
                $('H2.drawer-handle').find('IMG').attr("src","RightArrow.gif");
            $(this).find('IMG').attr("src","DownArrow.gif");
            });
});
0
 
upchelpdeskAuthor Commented:
That code works but does not change the arrow back to the RightArrow after it has been closed.  How do I do that?  

Also, I would like the closed menu items to shrink in height to  18px when closed with the open menu height of being 28px.  I have tried the code below but it just makes every menu item shrink to 18px and not keep the open one 28px.  Please help.  


$(document).ready(function () {
            $('LI.drawer UL').hide(); // hide all ULs inside LI.drawer 
            $('H2.drawer-handle').click(function () {
                $('LI.drawer UL:visible').slideUp().prev().removeClass('open').addClass("MarkClosed");  // open selected menu item and mark it
                $(".MarkClosed").css({ height: "28px" });
                $("H2.drawer-handle").css({ height: "18px" });
                $(this).not(".MarkClosed").addClass('open').next().slideDown();  // open selected menu item if it has not been marked
                $(".MarkClosed").removeClass("MarkClosed"); // if marked close and remove marked
                $(this).not(".MarkClosed").find('IMG').attr("src", "RightArrow.gif");
                $(this).not(".MarkClosed").find('IMG').attr("src", "DownArrow.gif");

            });
        });

Open in new window

0
 
StealthyDevCommented:
Hi, use the below code:
$(document).ready(function () {
	$('LI.drawer UL').hide(); // hide all ULs inside LI.drawer except the first one
	$('H2.drawer-handle').click(function () {
		$('LI.drawer UL:visible').slideUp().prev().removeClass('open').addClass("MarkClosed");
		$(this).not(".MarkClosed").addClass('open').next().slideDown();
		$('H2.drawer-handle').find('IMG').attr("src","Aiga_rightarrow.gif");
		$(this).not(".MarkClosed").find('IMG').attr("src","Aiga_leftarrow.gif");
		$(".MarkClosed").removeClass("MarkClosed");
	});
});

Open in new window

0
 
upchelpdeskAuthor Commented:
Very helpful easy to follow solution, thanks a lot.  
0
 
upchelpdeskAuthor Commented:
Ok, one more question, When I close them menu item, I need the whole menu to return to its orginal height.  So if the menu height is 38px per item, and the on click height when I menu is open is 28 px for that item and the closed items shrink to 18px, if the active menu is them closed I need all menu items to go back to 28 px.  

How do I do that?  
0
 
StealthyDevCommented:
Hi, I am not sure what you are looking for.. But I assume:
Heights: 45px-55px-65px
1. If an item is open its height should be 65px
2. If an item is closed, its height should be 45px
3. If an item is neither open, nor closed, its height should be 55px (default)
4. If all the items are closed, all height should come back to default (55px)

Update below stuff to achieve it !!!

Your new script:
      $(document).ready(function () {
            $('LI.drawer UL').hide();
            $('H2.drawer-handle').click(function () {
                  $('LI.drawer UL:visible').slideUp().prev().removeClass('open').addClass("closed").addClass("MarkClosed");
                  $('H2.drawer-handle').find('IMG').attr("src","Aiga_rightarrow.gif");
                  
                  $(this).not(".MarkClosed").addClass('open').next().slideDown();
                  $(this).not(".MarkClosed").find('IMG').attr("src","Aiga_leftarrow.gif");
                  
                  if($(this).not(".MarkClosed").html() == null)
                        $('H2.drawer-handle').removeClass('closed');
                        
                  $(".MarkClosed").removeClass("MarkClosed");
            });
      });

Update your styles:
    H2.drawer-handle {
        margin: 0;
        padding: 10px;
        background-color: #BAB1AD;
        height: 55px;
        margin-bottom: 3px;
        list-style: none;
            border: 1px solid red;
    }
   
    H2.drawer-handle.open {
        background-color: #BAB1AD;
        list-style: none;
            height: 65px;
            border: 1px solid blue;
    }
      
      H2.drawer-handle.closed {
            height: 45px;
            border: 1px solid green;
    }

Remove "open" from here:
<H2 class="drawer-handle open"><IMG src="Aiga_rightarrow.gif" style="height:10px;width:10px;"/>     WORSHIP</H2>
<H2 class="drawer-handle"><IMG src="Aiga_rightarrow.gif" style="height:10px;width:10px;"/>     WORSHIP</H2>

Best Regards.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.