Clickout in jquery
Asked Answered
D

7

6

What is the easiest way to do a clickout in jquery Where a div box hides if clicked anywhere outside it.

Douma answered 28/1, 2011 at 20:45 Comment(0)
L
12

I don't like the solutions that use stopPropagation because often I am using event bubbling to manage various links. I don't want to have to worry that they might stop working if the box exists. My choice would look something like this:

var $box = $('#box');
$(document.body).click(function(){
    if (!$box.has(this).length) { // if the click was not within $box
        $box.hide();
    }
});

The has function filters the selection and returns elements only if they contain the element passed as the parameter (you can also use a selector string, but that isn't relevant here). If the click was outside the box, length will be 0 so the conditional will pass and the box will be hidden.

This should be optimised by setting a boolean value for whether the box is currently visible and only doing the has call if it is currently visible.

Loiretcher answered 28/1, 2011 at 21:37 Comment(1)
This didn't work for me, but after looking at Caspar's answer I got it to work by changing this to e.target and adding e as an argument to the callback.Paresthesia
V
5

like this:

$("#thediv").show(0, function() {
   var that = $(this);
   $(document).bind("click", function(e) {
       if($(e.target).attr("id") != that.attr("id")) {
          that.hide();
          $(this).unbind("click");
       }
    });
});

fiddle here: http://jsfiddle.net/XYbmE/3/

Vireo answered 28/1, 2011 at 20:57 Comment(0)
M
3
$('body').click(function() {
    $('#box').fadeOut();
});

$('#box').click(function(e) {
    e.stopPropagation();
});

This works because #box will receive the click event first if you clicked inside. Since propagation is stopped, the handler attached to body won't receive it so the box won't be closed unless you clicked outside of the box.

If you want it to hide immediately, use .hide()

I've got the stopPropagation() trick from How do I detect a click outside an element?

Marmolada answered 28/1, 2011 at 20:52 Comment(1)
This would hide it if you click inside the div, not outside.Snook
J
2

I recommend use this lib https://github.com/gsantiago/jquery-clickout. It's awesome!

$('button').on('clickout', function (e) {
  console.log('click outside button')
})
Joeyjoffre answered 27/12, 2016 at 11:51 Comment(0)
C
1

I know that jQuery has the "focusout" event for form elements, not sure if it could maybe be used for a

Cohdwell answered 28/1, 2011 at 20:56 Comment(0)
D
0

Bind a click function to the document itself:

$(document).click(function(){
    alert('doc');
});

Then bind a function to the same event on the div itself and return false so the event will not bubble up to the document.

$('#mydiv').click(function(e){
    alert('mydiv click');
    return false;
}
Disembody answered 28/1, 2011 at 20:58 Comment(1)
Better use e.stopPropagation() instead of return false;.Marmolada
S
0

Heres my solution

    var ClickOut = {};

    ClickOut.list = [];

    ClickOut.Add = function(jqel, callback) {
        var i = ClickOut.list.push({jqel:jqel, callback:callback});
        return (i-1);
    }

    ClickOut.Remove = function(i) {
        ClickOut.list.splice(i, 1);
    }

    ClickOut.Init = function() {
        $('body').click(function(e) {
            for(var x = 0; x < ClickOut.list.length; x++) {
                if (!ClickOut.list[x].jqel.has(e.target).length) {
                    ClickOut.list[x].callback();
                }
            }
        });
    }
Sagacity answered 18/10, 2012 at 14:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.