If you need to hide a div when you click outside of it. Below snippets are explaining that behavior in detail.
HTML
<div class="main-container">
<div> Hello I am the title</div>
<div class="tobehide">I will hide when you click outside of me</div>
</div>
jQuery
jQuery(document).click(function(e) {
var target = e.target; //target div recorded
if (!jQuery(target).is('#tobehide') ) {
jQuery(this).fadeOut(); //if the click element is not the above id will hide
}
})
Its pretty self explanatory, when you click outside of the target div, the jQuery function check for then click event is based on the element. If not the div will fade out.
- Just want to thank us? Buy us a Coffee
- May be another day? Shop on Amazon using our links.
Your prices won't change but we get a small commission.
Leave a Reply