I have a floating div that gets displayed, and I want it to be hidden when the user clicks off the div. This would be similar to the .hover() function callback when hoverin
Surely you're looking for the blur event?
You can try this. http://benalman.com/projects/jquery-clickoutside-plugin/
Using an event handler on the document works well for me:
function popUp( element ) { element.onmousedown = function (event) { event.stopPropagation(); }; document.onmousedown = function () { popDown( element ); }; document.body.appendChild( element ); } function popDown( element ) { document.body.removeChild( element ); document.onmousedown = null; }
If you want to clear the div when you click somewhere else in the page, you can do something like:
$('body').click(function(event) {
if (!$(event.target).closest('#myDiv').length) {
$('#myDiv').hide();
};
});
$('body').click(function (event) {
if ($("#divID").is(':visible')) {
$('#divID').slideUp();
}
});
This can be used to check if the div is visible, if it is visible, it will then slide the object up.
I've found the solution in a forum... but I can't find it back to credit the orginal author. Here is the version (modified that lives in my code).
$(document).bind('mousedown.yourstuff', function(e) {
var clicked=$(e.target); // get the element clicked
if( clicked.is('#yourstuff')
|| clicked.parents().is('#yourstuff')) {
// click safe!
} else {
// outside click
closeIt();
}
});
function closeIt() {
$(document).unbind('mousedown.emailSignin');
//...
}
I also have ESC keyup bindings and a 'close' html anchor not pictured above.