I am implementing popovers, since the existing libraries are not flexible enough. I want to set any div with a class .popover
and it shows the behaviors:
- destroy on escape key
- destroy when clicks outside borders
Both are events, and both need to apply on elements classed as .popover
. To destroy popovers on escape:
$(document).keyup(function(e) {
if (e.keyCode == 27) {
popovers = $(".popover");
//destroy popovers
}
});
But I am stuck with how to listen in to mouse clicks globally and determining if they happened outside of a .popover
.
SO answers mention having a global click listener that destroys all .popover
s and adding a click listener to each popover that halts event propagation to the global listener.
But I want to define it only once globally, not once for every single popover I make. How do I do that?