best in jQuery. thanks.
views:
32answers:
3
+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
2010-07-30 11:41:16
it's a clever method, thank you nick.
lovespring
2010-07-30 11:45:56
+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
2010-07-30 11:43:13
IMO that plugin is tremendous overkill for a *very* simple problem :)
Nick Craver
2010-07-30 11:45:25
+1 for using $.contains(), pretty rare stuff. I would give you +2 if I could.
jAndy
2010-07-30 11:48:42
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
2010-07-30 12:04:40
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
2010-07-30 11:46:42