20

This question has been asked multiple times, however none of the answers seem to work for me.

The css of the div is as follows:

#info{
  display: none;
  position: fixed;
  z-index: 500;
  height: 50%;
  width: 60%;
  overflow: auto;
  background: rgba(187, 187, 187, .8);
}

I tried to use the following code:

$("#info").click(function(e){
  e.stopPropagation();
});

$(document).click(function(){
  $("#info").hide();
});

as well as this code:

$(document).mouseup(function (e){
    var container = $("#info");

    if (container.has(e.target).length === 0) {
        container.hide();
    }
});

Yet whenever i click on the div it also disappears, no clue why but it does.
Any thing else that might work?

thecodeparadox
  • 86,271
  • 21
  • 138
  • 164
Gooey
  • 4,740
  • 10
  • 42
  • 76

7 Answers7

37

As your target has id=info, so you can try:

$(document).click(function(e) {

  // check that your clicked
  // element has no id=info

  if( e.target.id != 'info') {
    $("#info").hide();
  }
});

You can also try:

$(document).click(function() {

  if( this.id != 'info') {
    $("#info").hide();
  }

});

According to comment

$(document).click(function(e) {

    // check that your clicked
    // element has no id=info
    // and is not child of info
    if (e.target.id != 'info' && !$('#info').find(e.target).length) {
        $("#info").hide();
    }
});
thecodeparadox
  • 86,271
  • 21
  • 138
  • 164
  • One question tough (it works perfectly so far) however when you click on a

    tag or just text it also disappears (logic). Is there a fix for this too?

    – Gooey Jul 18 '12 at 17:01
  • @Gooey i.e `h1` tag or `text` within `info` div? – thecodeparadox Jul 18 '12 at 17:08
  • Yes, if i click now on text (either plain text or text between the

    tags) the div also hides. Is there a way to let the "children" of this div also be part of the "dont hide" function?

    – Gooey Jul 18 '12 at 17:17
  • Thanks, the updated code works fine with child elements. – Jurica Krizanic Apr 25 '15 at 15:50
5

To ensure you have a solution that works on iPads too, then you need to use the following function trigger instead:

$(document).on("mousedown touchstart",function(e){
  var $info = $('#info');
  if (!$info.is(e.target) && $info.has(e.target).length === 0) {
    $info.hide();
  }
});

Similarly, if you are looking to cover off mouseup, add 'touchend':

$(document).on("mouseup touchend",function(e){
  ...
});
Andy Lorenz
  • 2,905
  • 1
  • 29
  • 29
4

Attach an onclick event handler to the document object:

$(document).click(function(e) {   
    if(e.target.id != 'info') {
        $("#info").hide();   
    } 
});

Demo: http://jsfiddle.net/aUjRG/

Here is a solution in pure JavaScript to help you better understand what it happening:

function hideInfo(){
    if(window.event.srcElement.id != 'info'){
        document.getElementById('info').style.display = 'none';
    }
}

document.onclick = hideInfo;

Demo: http://jsfiddle.net/mmzc8/

Both solutions will check if the location that the user clicked was on the element with an ID of info. Assuming that the user did not click on the info element, then hide the info element.

Robert
  • 8,717
  • 2
  • 27
  • 34
3

Try this code, this is the best for me.

jQuery('.button_show_container').click(function(e) {
    jQuery('.container').slideToggle('fast'); 
    e.stopPropagation();
});

jQuery(document).click(function() {
        jQuery('.container').slideUp('fast');
});
Ken Avila
  • 1,091
  • 1
  • 8
  • 10
0

You can add a class only for check on mouse click that specific div or any within that div element clicked or not.

$(document).click(function(e){            
    if ( !$(e.target).hasClass("[class name for check]") &&
         $("[element class or id]").css("display")=="block" ) {
            //...code if clicked out side div
    }
});
Axel
  • 3,331
  • 11
  • 35
  • 58
0

A solution with plain JavaScript, without need of JQuery:

const hide = (elem) => {
    elem.style.display = 'none';
};

document.addEventListener('click', (event) => {
  const menuContainer = document.getElementById('menuContainer');
  if (!menu.contains((event.target))) {
    hide(menuContainer)
  }
});
vanderdill
  • 162
  • 2
  • 14
-1

Try the below solution. It's even working recursive:

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

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

Reference - https://stackoverflow.com/a/7385673/3910232

Sam G
  • 1,242
  • 15
  • 12