www.webdeveloper.com
Results 1 to 3 of 3

Thread: jquery menu toggle- menu closes straight away or clicking on a menu item closes it..

  1. #1
    Join Date
    Oct 2013
    Posts
    6

    Question jquery menu toggle- menu closes straight away or clicking on a menu item closes it..

    I know this has been asked and answered many times but I can't seem to be able to apply other topics to make it work for me.. so hopefully someone can help me- it feels so close and yet so far..

    I've got the jquery script below to create mobile menu toggle, and I used css media queries to hide/show it on different screen widths. This code below makes menu appear when I click on the toggle button but it slides back up straight away:

    Code:
    jQuery(document).ready(function($) {
        $("#menu-menu-2").hide();
        $(".mtoggle").click(function() {
            $("#menu-menu-2").slideToggle(500);           
        });
    });
    I tried adding

    Code:
    return false
    which kept the menu open once I clicked toggle button, but clicking on the menu item only closed the menu, but kept me on the same page.

    If I understand it correctly the click on the menu item 'activates' the click on the toggle button and so it closes the menu, but I can't find a way to stop it from happening..?

    I tried adding this to the jquery code above, instead of 'return false':

    Code:
    $( "#menu-menu-2" ).click(function( event ) {
      event.stopPropagation();
    });
    but the menu slides up straight after opening..

    Would really appreciate your help as I feel very stuck!

    P.S. the website is in maintenance mode so can't share it, sorry!

  2. #2
    Join Date
    Oct 2013
    Posts
    6
    This worked, maybe it will help someone in the future

    Code:
    jQuery(document).ready(function($) {
            $("#menu-menu-2").hide();
            $(".mtoggle").click(function() {
                $("#menu-menu-2").slideToggle(500);
                         
                return false;
            });
            
            
         $("#menu-menu-2 li a").click(function(event) {
                event.stopPropagation();
                });
        });

  3. #3
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,035
    In future, please take the time to post in the right forum, the site has a JQuery section in the frameworks forum, thanks.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles