Skip to content

Instantly share code, notes, and snippets.

@lkwdwrd
Created October 23, 2013 18:24
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 lkwdwrd/7123917 to your computer and use it in GitHub Desktop.
Save lkwdwrd/7123917 to your computer and use it in GitHub Desktop.
// close something on document click
// Non jQuery
function closeSomething( event ) {
var id = 'ID-of-container';
function traverseDOM( el ) {
if ( null === el.parentNode ) {
return true;
} else if ( id === el.id ) {
return false;
} else {
return traverseDOM( el.parentNode );
}
}
if ( traverseDOM( event.target ) ) {
// close the thing.
something.close();
}
}
document.addEventListener( 'click', closeSomething );
// jQuery
function closeJQ( event ) {
var id = 'ID-of-container';
if ( $( event.target ).parents( '#' + id ) ) {
// close the thing.
something.close();
}
}
document.on( 'click', closeJQ );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment