0

let me explain what i do need. I have a navigation which opens by jquery. And i would like to close it when i click anywhere in the site.

I found this one here, but in this case every click would close the navigation: jQuery click() event catch-all?

using

$(document).click(function() {
  $("#boxID").hide();
});

But when i click within the navigation e.g. id="boxnewsID", the navigation id="navID" itself closes too. How can i prevent this?

When i try to get a id or sth. which was clicked it only gives the document (logic) back. I have seen, that facebooks header navigation (message, activity, friendrequest) works the way i need.

Any idea?

Community
  • 1
  • 1
Ruven JR. Maerson
  • 309
  • 1
  • 6
  • 16

2 Answers2

0

To exclude one or more elements, you can use this:

$(document).not("#boxID").click(function() { $("#boxID").hide(); });

This excludes the element with ID #boxID form your selection.

looper
  • 1,929
  • 23
  • 42
0

I think this should do it:

$(document).on("click", ":not(#boxID)", function() {
    $("#boxID").hide();
}

EDIT:

That doesn't work, either. Here's what I have in a page of mine:

$(document).mousedown(function(e) {
    if ($(e.target).closest("#boxID").length == 0) {
        $("#boxID").hide();
    }
});
Barmar
  • 741,623
  • 53
  • 500
  • 612