Last active
December 22, 2019 16:05
-
-
Save mynameispj/5182844 to your computer and use it in GitHub Desktop.
Simple jQuery dropdown boxes that disappear when you click outside of them
This file contains 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
<a class="drop-down-toggle">Click me to reveal drop down box below...</a> | |
<div class="drop-down-wrapper"> | |
Hello, I will be revealed! | |
</div> |
This file contains 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
$('a.drop-down-toggle').click(function(){ | |
myDropDown = $(this).next('.drop-down-wrapper') | |
if( myDropDown.is(':visible') ) { | |
$(this).removeClass('drop-down-open'); | |
myDropDown.hide(); | |
} else { | |
myDropDown.fadeIn(); | |
$(this).addClass('drop-down-open'); | |
} | |
return false; | |
}); | |
$('html').click(function(e) { | |
$('.drop-down-wrapper').hide(); | |
}); | |
$('.drop-down-wrapper').click(function(e){ | |
e.stopPropagation(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks!
For validation, line2 (missig var + semicon)
var myDropDown = $(this).next('.drop-down-wrapper');