Created
October 23, 2013 18:24
-
-
Save lkwdwrd/7123917 to your computer and use it in GitHub Desktop.
// close something on document click
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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