Skip to content

Instantly share code, notes, and snippets.

@MSerj
Last active April 13, 2018 15:41
Show Gist options
  • Save MSerj/54319b4e75a026e2fb159d8ee9e61adf to your computer and use it in GitHub Desktop.
Save MSerj/54319b4e75a026e2fb159d8ee9e61adf to your computer and use it in GitHub Desktop.
close div by click outside
$(document).mouseup(function (e) {
var container = $("YOUR CONTAINER SELECTOR");
if (!$(e.target).closest(container).length){
container.hide();
}
});
//что означает следующее - если клик был по области, которая НЕ является нашим div'ом или не содержится в нем, то скрыть блок.
//Это решает проблему, если клик был по элементу вложенному в блок (не по самому блоку). Элемент будет скрыт, только если клик по области ВНЕ div'a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment