robert ioersed
robert ioersed

Reputation: 1

How to hide element when click outside area using javascript?

How to hide element when click outside area using javascript ?

http://jsfiddle.net/a3MKG/35/

I try like this but not work

<script>
function showDiv(id) {    
  $("#div1").toggle();
  $(document).click(function() {
    $('#div1').fadeOut(300);
});
}   
</script>

Upvotes: 0

Views: 65

Answers (1)

Arun P Johny
Arun P Johny

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

Related Questions