Using jQuery how does one detect clicks not on specific elements, and perform an action consequently?
I have the following JavaScript
$('#master').click(function() {
$('#slave').toggle();
});
$(document).not('#master','#slave').click(function() {
$('#slave').hide();
});
and I cannot see where I am going wrong logically. You can see a live example here
Since you’re binding to the
clickevent on the document, you can use event.target to get the element that initiated the event:EDIT: As Mattias rightfully points out in his comment, the code above will fail to identify click events coming from descendants of
#masterand#slave(if there are any). In this situation, you can use closest() to check the event’s target: