hide div if clicked outside of it [duplicate]

EDIT : A better approach here:
How to create a custom modal popup – and how to close it clicking outside of it



jsBin demo

$( "#idSelect" ).click(function( e ) {
   e.stopPropagation();
   $("#idDiv").toggle();
});

$( "#idDiv" ).on('click',function( e ) {
     e.stopPropagation();
});

$( document ).on('click', function( e ) {
   if( e.target.id != 'idDiv' ){
       $( "#idDiv" ).hide();
   }   
});

Leave a Comment