Is there an event that I can tie into that is fired when a bootstrap dropdown is closed or toggled?
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
- Can php detect if javascript is on or not?
This is how Bootstrap v2.3.2 closes the menu no matter what you click on:
If you're using v2.x, this could be used to know when a menu will be closed. However, keep in mind that this is triggered on every click. If you only need to execute something when a menu is really closed (which is probably all the time), you'll need to track when one is opened in the first place. The accepted answer is probably a better solution in that regard.
In Boostrap v3.0.0, however, the drop menu supports four separate events:
From Bootstrap's documentation.
From twitter bootstrap official page:
hide.bs.dropdown
is one of 4 events described here.Update (13-Apr-16)
These events also work same in
Bootstrap 4
. Bootstrap v4 Documentation.There isn't a documented event, but you can use the
.open
class of the.dropdown
and the jQueryclick()
event:For toggling, use just the
click()
event.Here is the jsfiddle.
I did mine as shown below.
HTML:
Script
Works great
Nolan, I assume what you mean by "This did not work" is: when the user clicks anywhere else on the page and not properly closes the button/dropdown. You can monitor those clicks (anywhere on the document) by:
In the end, the only reliable method that I found was to use jquery's data api to store the state of the dropdown and add click events to the button and the document.