tags:

views:

76

answers:

3

I have a div, and when you click on it, used toggle() and shows and hides a list div underneath the button. I want it so that when you click anywhere on the page (not in that div*) it closes. I realize that this shouldn't work with toggle. Can someone please point me in the right direction.

I want to click a button then rather than having to click the button again to close the div I want to be able to click anywhere and it closes.

+5  A: 

You can stop the click event from bubbling, like this:

$("#divID, #buttonID").click(function(e) {
  e.stopPropagation();
});
$(document).click(function() {
  $("#divID").hide();
});

Using event.stopPropagation() a click from the "button" (which seems to be another <div>?) or from the div won't bubble up to document, anywhere else will...and when the click gets there, it'll close the <div>. So a click from outside the <div> closes, a click from inside does nothing.

Nick Craver
@Nick - Why is it that the function argument `e` is the event; even though, it's passing into the function doesn't seem to be specified anywhere?
Peter Ajtai
@Peter - the event is by default the first argument passed to every event handler in jQuery, just the format/convention they use :) Firefox does this as well, same idea there :)
Nick Craver
A: 
$("*").not("#specialDiv").click(function() {
     $('#specialDiv').hide();
})
Zane Edward Dockery
This is a *very*, **very** bad way to solve the problem, this adds a click handler to every element in the document. ...strongly considering upping my downvote count for this.
Nick Craver
-1 Registering a handler for **all** elements is pretty expensive, both in time and memory. Using bubbling (which you cannot turn off anyway) seems much faster and more like The Right Thing (TM). @Nick Craver: why don't you?
MvanGeest
@Mvan - You're right, except that you stated that you cannot turn off bubbling. Doing `return false` or `event.stopPropagation()` in a handler turns off bubbling at the point of that handler.
patrick dw
@patrick dw - I meant that you cannot stop the bubbling mechanism as a whole (to gain performance); you can only stop the bubbling of a specific event. You can, though, get rid of this to gain performance :)
MvanGeest
@MvanGeest, @patrick - I'm tempted to, this user is providing very bad answers to any jQuery questions, I'm not sure what the correct action is really, but the majority of his answers are 1) wrong, 2) terribly inefficient or 3) would just outright throw an error...just look through his answer history.
Nick Craver
@Mvan - Ah, now I see what you meant. :o)
patrick dw
@Nick - You're always very good about giving notice on poor answers. It's a shame that many users don't delete or modify the ones that are pointed out as being incorrect/unadvisable. Leaves a lot of junk on SO. If you want to maintain your good record of restraint, I think future readers will get the idea from the comments. :o)
patrick dw
+1  A: 

return false is your friend here, it prevents default behavior, otherwise the document click event is fired when clicking the button or the div.

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script src="http://code.jquery.com/jquery-1.4.2.min.js" type="text/javascript"></script>
    <script type="text/javascript">
    $(function () {
        $("button").click(function () { $("div").toggle(); return false; });
        $(document).click(function () { $("div").hide(); });
        $("div").click(function () { return false; });
    });
    </script>
</head>
<body>
    <button>Toggle me!</button>
    <div style="height: 400px; width: 400px; background: red;"></div>
</body>

Godisemo
I was just about to ask about this. thanks... Do I really need e.propagation I really don't know what it's doing.
ThomasReggi
This could be used on links to to prevent the default link behavior. I use it a lot with ajax.
Godisemo
@ThomasReggi - `stopPropgation()` just prevents the bubbling, click the "bubble up" link in my answer for a very detailed explanation. Doing a `return false` stops the event dead in it's tracks, preventing *any* other handlers from running, if you're not doing much with other handlers, either may work equally fine. If you want to steer clear from interfering with other handlers, use `.stopPropagation()` to be safe.
Nick Craver
I'm using Div's for buttons. I copied and pasted the code twice and changed all the selectors, none of them are the same. When I click one link is shows the other div, click anywhere it closes but if I click the other button is stays open. Why is this?
ThomasReggi