STYLE SWITCHER

Use jQuery to hide a DIV when the user clicks outside of it

$('body').click(function(evt){  
	var eventId = $(event.target).attr('class');
	
       if(eventId == "mobile-menu")
          return;
       //For descendants of menu_content being clicked, remove this check if you do not want to put constraint on descendants.
       if($(evt.target).closest('.cus-popup').length)
          return;             
		$(".cus-popup").slideUp(200);
      //Do processing of click event here for every element except with id menu_content

});

Another simple method

 $(document).mouseup(function(e) 
 {
    var container = $("YOUR CONTAINER SELECTOR");

    // if the target of the click isn't the container nor a descendant of the container
    if (!container.is(e.target) && container.has(e.target).length === 0) 
    {
        container.hide();
    }
});

Recent Article

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  • Keep your skills sharp, get informed.

    Subscribe to our mailing list and get interesting stuff and updates to your email inbox