0

Hi everyone i want to learn how to use jquery fadeToggle for my DEMO

In this demo you can see there is a click show div div. When you click this div then .gelen-mesaj-alani is showing. My question is how can a hide this div when clicked other area ?

$(document).ready(function(){
  $('.click').on('click',function(){
     $(".gelen-mesaj-alani").fadeToggle(300);
  });                  
});

2 Answers2

1

You can bind the click event on body. if target have no closest element with class .click or .gelen-mesaj-alani then hide the element:

$('body').on('click', function(e) {
 if($(e.target).closest('.click').length == 0 && $(e.target).closest('.gelen-mesaj-alani').length==0) {
    // click happened outside of menu, hide any visible menu items
   $(".gelen-mesaj-alani").fadeOut(300);
}});

Working Demo

Milind Anantwar
  • 81,290
  • 25
  • 94
  • 125
0

Possible duplicate of: Use jQuery to hide a DIV when the user clicks outside of it

$(document).mouseup(function (e)
{
    var container = $("YOUR CONTAINER SELECTOR");

    if (!container.is(e.target) // if the target of the click isn't the container...
        && container.has(e.target).length === 0) // ... nor a descendant of the container
    {
        container.hide();
    }
});
Community
  • 1
  • 1
yakutsa
  • 642
  • 3
  • 13