I am using jquery’s slidetoggle, want to learn how to make the showup class hide when click anywhere outside of the DIV.
thanks!
Online SAMPLE: http://jsfiddle.net/evGd6/
<div class="click">click me</div>
<div class="showup">something I want to show</div>
$(document).ready(function(){
$('.click').click(function(){
$(".showup").slideToggle("fast");
});
});
.showup {
width: 100px;
height: 100px;
background: red;
display:none;
}
.click {
cursor: pointer;
}
Stop event propagation from within the
.showuparea:Then prevent those clicks on
.showupfrom bubbling up to thedocument:Any click event that reaches the
documentwill result in the.showupelement being hidden. Any click events that start from within.showupwill be prevented from proceeding any further up the DOM tree, and thus will never reach thedocument.You will also need to stop any clicks on your button from traveling up to the
documentas well:Otherwise that click event will bubble up to the
documentand result in the hiding of.showupimmediately.Demo: http://jsfiddle.net/evGd6/2/