Skip to content

Instantly share code, notes, and snippets.

@mynameispj
Last active December 22, 2019 16:05
Show Gist options
  • Save mynameispj/5182844 to your computer and use it in GitHub Desktop.
Save mynameispj/5182844 to your computer and use it in GitHub Desktop.
Simple jQuery dropdown boxes that disappear when you click outside of them
<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>
$('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();
});
@dragoeco
Copy link

Thanks!
For validation, line2 (missig var + semicon)
var myDropDown = $(this).next('.drop-down-wrapper');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment