div
variable in your code refers to the .inner
element, so it doesn't have .inner
child element. Based on the structure of markup, has
method is not useful here. You can use is
method:
$(document).mousedown(function(e) {
if ( !$(e.target).is('.inner') ) {
alert('clicked outside of .inner');
}
});
The above method doesn't work if the .inner
has children elements, for this case you can use closest
method:
$(document).mousedown(function(e) {
if ( !$(e.target).closest('.inner').length ) {
alert('clicked outside of .inner');
}
});