1

this is my code for a menu that comes in from the right side... heres is the js

jQuery(document).ready(function($) {
    var $toggleButton = $('.toggle-button'),
        $menuWrap = $('.menu-wrap'),
        $sidebarArrow = $('.sidebar-menu-arrow');
            $content = $('.content');
    // Hamburger button
    $toggleButton.on('click', function() {
        $(this).toggleClass('button-open');
        $menuWrap.toggleClass('menu-show');
        $content.toggleClass('content-background');
    });

    // Sidebar navigation arrows
    $sidebarArrow.click(function() {
        $(this).next().slideToggle(300);
    });
});

i want it to close automatically when clicked outside anywhere out side the menu. how can i do this.

The Naga Tanker
  • 441
  • 1
  • 4
  • 17
  • possible duplicate: http://stackoverflow.com/questions/1403615/use-jquery-to-hide-a-div-when-the-user-clicks-outside-of-it – Kapila Perera Mar 15 '17 at 05:29
  • Possible duplicate of [Use jQuery to hide a DIV when the user clicks outside of it](http://stackoverflow.com/questions/1403615/use-jquery-to-hide-a-div-when-the-user-clicks-outside-of-it) – Mariy Mar 15 '17 at 05:49

2 Answers2

0

Simply do as follow:

       $(document).click(function (e)
        {
            var container = $(".toggle-button");
            if ((!container.is(e.target)))     // if the target of the click isn't the TOGGLE BUTTON... 
            {
                // Code to hide the menu
                $('.toggle-button').toggleClass('button-open');
                $('.menu-wrap').toggleClass('menu-show');
                $('.content').toggleClass('content-background');
            }
        });

That's it
Enjoy coding :)

Amrinder Singh
  • 5,300
  • 12
  • 46
  • 88
-2

Hope this helps you.

$('body').click(function() {
   //add your code here to hide the menu
});