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.
You can stop the
clickevent from bubbling, like this:Using
event.stopPropagation()a click from the “button” (which seems to be another<div>?) or from the div won’t bubble up todocument, anywhere else will…and when theclickgets there, it’ll close the<div>. So a click from outside the<div>closes, a click from inside does nothing.