views:

32

answers:

3

best in jQuery. thanks.

+1  A: 

You can use event bubbling to your advantage here, like this:

$("#tableID").click(function(e) {
  e.stopPropagation();
});
$(document).click(function() {
  $("#tableID").hide();
});

With event.stopPropagation() we stop the bubble from reaching document when it comes from within the table. When it comes from elsewhere it (by default) gets to document, hiding the table. In "by default", I mean you haven't done areturn false or .stopPropagation() on those click events.

Nick Craver
it's a clever method, thank you nick.
lovespring
+3  A: 

If you're asking how to hide the table when the user clicks elsewhere in the document (your wording is a little off), then may I suggest the clickoutside special event from Ben Alman?

Usage:

$('table').bind("clickoutside", function(event){
    $(this).hide();
});

Or, if that seems a bit OTT, then try this (no plugins required):

var myTable = $('table');
$(document).click(function(e) {
    if (e.target !== myTable[0] && !$.contains(myTable[0], e.target)) {
        myTable.hide();
    }
});
J-P
IMO that plugin is tremendous overkill for a *very* simple problem :)
Nick Craver
Just added an alternate solution. I agree tbh.
J-P
+1 for using $.contains(), pretty rare stuff. I would give you +2 if I could.
jAndy
It should be noted that `$.contains()` is *much* more expensive, in every case but especially in older browsers then finding the element, and attaching a single handler. IE (including 8) for example doesn't support `compareDocumentPosition` which makes it even more expensive and recursive on *each* click event, rather than a one time (and cheaper even then) startup cost to bind it.
Nick Craver
thank you, j-p.
lovespring
A: 

Table have not blur, but elements inner table have:

$('#yourtableid:visible a').each(function(){
    $(this).bind('blur', function(){
        $('#yourtableid:visible').hide();
    });
});

$(document).bind('click', function(){
   $('#yourtableid:visible').hide();
});

This is jast idea, not solution

HWTech
thank you, HWTech.
lovespring