Reputation: 1
How to hide element when click outside area using javascript ?
I try like this but not work
<script>
function showDiv(id) {
$("#div1").toggle();
$(document).click(function() {
$('#div1').fadeOut(300);
});
}
</script>
Upvotes: 0
Views: 65
Reputation: 388436
You can use a click handler to the document object where if the click has not originated from the div or button hide the div
$(document).click(function(e){
if(!$(e.target).closest('#div1, input[name="Showdiv1"]').length){
$('#div1').hide()
}
})
Demo: Fiddle
Upvotes: 1