Friday, September 21, 2018

jQuery: hide element when clicking outside the div

Read code comments for details
     //hide menu options when clicking outside the div
    $(document).mouseup(function(e) {
      var container = $(".profile-opts");
      var reportBtn = $( ".ar-report-btn" );

      // if the target of the click isn't the container
      // nor a descendant of the container
      // nor the report button itself
      if (!container.is(e.target) && container.has(e.target).length === 0 && !reportBtn.is(e.target) && reportBtn.has(e.target).length === 0) {
            $( ".profile-opts" ).fadeOut( "faster" );
      }
    });

No comments:

Post a Comment