Skip to content

Instantly share code, notes, and snippets.

@lewiswalsh
Created March 18, 2015 15:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lewiswalsh/c491a059800d18f0e811 to your computer and use it in GitHub Desktop.
Save lewiswalsh/c491a059800d18f0e811 to your computer and use it in GitHub Desktop.
jQuery: Hide on click outside of element
$(document).on('mouseup', function(e){
var my_element = $('<my_element_selector>');
if(
!my_element.is(e.target) // if the target of the click isn't the live_search_results...
&& my_element.has(e.target).length === 0 // ...nor a descendant of the live_search_results
&& my_element.is(":visible") // Is it even showing on screen?
){ my_element.slideUp(); }
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment