how to hide a side nav on click

Discussion in 'HTML & Website Design' started by Silvester Vella, Feb 3, 2015.

  1. #1
    I have this code for a side nav. How do I make the #menu disappear again when the #menu a is clicked?

    Thanks.

    function sideMenu() {

    $("#showmenu").click(function(e){
    e.preventDefault();
    $("#menu").toggleClass("show");
    });

    $("#menu a").click(function(event){
    event.preventDefault();
    if($(this).next('ul').length){
    $(this).next().toggle('fast');
    $(this).children('i:last-child').toggleClass('fa-caret-down fa-caret-left');
    }
    });
    }
     
    Silvester Vella, Feb 3, 2015 IP
  2. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #2
    Very hard to say without seeing the markup being manipulated by that scripting, and any educated guesses just mean more questions like:

    1) If they're anchors, clicking on them should be taking you to another page "restoring" the state, so unless you're pissing away your accessibility with some poorly written non gracefuly degrading ajaxtardery...

    2) If that's a normal side menu, or even one of the goofy "replace it with a useless icon for responsive" menus you shouldn't be throwing scripting at it for NOTHING.

    3) Of course if you're using the bloated pile of garbage known as jQuery, you probably should be shoveling dirt on the site and start over.

    Basically I'd need to see a whole lot more of the page in question, but even your little snippet is throwing up major "don't do that" warning flags.
     
    deathshadow, Feb 3, 2015 IP
  3. KewL

    KewL Well-Known Member

    Messages:
    245
    Likes Received:
    16
    Best Answers:
    3
    Trophy Points:
    128
    #3
    KewL, Feb 3, 2015 IP
  4. vselluk

    vselluk Member

    Messages:
    20
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    25
    #4
    why dont you use toggle function? (jQuery)
     
    vselluk, Feb 4, 2015 IP
  5. Silvester Vella

    Silvester Vella Greenhorn

    Messages:
    22
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #5
    Thank you all for your help.. Im very new at javascript and jquery but I added this code which did the trick.

    $("#menu a.menu-link").click(function(e){
    e.preventDefault();
    $("#menu").toggleClass("show");
    });

    there are ways to shorten it probably but Im happy with it for now.
     
    Silvester Vella, Feb 5, 2015 IP