Dev Blog
When using tooltips on buttons which have dropdown menus, the tooltip might not disappear as we would like to, leaving it visible. This might even block user action. To avoid this issue, tooltip need to be hidden when dropdown is shown. But it turned out that it is not enough, as it might pop up again. Also when dropdown is closed by another click, tooltips still might be left visible.
In summary
It turned out that tooltip must be hidden on events:
- Dropdown shown
- Dropdown hidden
- Mouse entering dropdown menu
Delegation
Delegated initialization
Example of delegated initialization:
$('body').tooltip({ container: 'body', selector:'[rel~="tooltip"]' })
To hide tooltip initialized this way, call tooltip
method with hide
parameter:
$('body [rel~="tooltip"]').tooltip('hide')
Putting it together
Having tooltip initialized, it is time to attach event handlers. The events we need to take care of are:
-
mouseenter
-
hidden.bs.dropdown
-
shown.bs.dropdown
The resulting code is pretty simple:
var handler = function() { return $('body [rel~="tooltip"]').tooltip('hide'); }; $(document).on('mouseenter', '.dropdown-menu', handler); $(document).on('hidden.bs.dropdown', handler); $(document).on('shown.bs.dropdown', handler);