I am trying to hide all other popovers when a new popover is selected by doing the following:
My HTML
a.btn#requests(rel='popover',data-placement='bottom',data-original-title='<b>Requests</b>',data-content='My content goes here')
My Javascript
$(function (){
console.log('start');
$('#requests').popover();
$('#messages').popover();
});
//This doesn't work for some reason?
$('#requests').on('show', function (e) {
console.log('requests');
$('#messages').popover('hide');
});
$('#messages').on('show', function () {
console.log('messages');
$('#requests').popover('hide');
});
However, my console.log('requests') and console.log('messages'); is never getting shown even though the requests and messages popovers are showing, what am I doing wrong?
To create a popover, add the data-toggle="popover" attribute to an element. Note: Popovers must be initialized with jQuery: select the specified element and call the popover() method.
Popovers are opt-in for performance reasons, so you must initialize them yourself. Zero-length title and content values will never show a popover. Specify container: 'body' to avoid rendering problems in more complex components (like our input groups, button groups, etc).
A Bootstrap Popover is an attribute in bootstrap that can be used to make any website look more dynamic. Popovers are generally used to display additional information about any element and are displayed with a click of a mouse pointer over that element.
To create a popover, you need to add the data-bs-toggle="popover" attribute to an element. Whereas, popover's title and its content that would display upon trigger or activation can be specified using the title and data-bs-content attribute respectively. Here is the standard markup for adding a popover to a button.
Bootstrap now supports popover events - see the Events section in the official popover docs (and here's the doc changelog).
Example:
$('#requests')
.popover()
.on('show.bs.popover', function() { console.log('o hai'); })
.on('hidden.bs.popover', function() { console.log('ciao'); });
The popover plugin doesn't trigger any event. Neither does the tooltip plugin (since popover extends tooltip). Check this issue (github) for alternatives.
You can use different JS events depending on your trigger
. For your example : Demo (jsfiddle)
$(function (){
console.log('start');
$('#requests').popover();
$('#messages').popover();
$('#requests').on('click', function (e) {
console.log('requests');
$('#messages').popover('hide');
});
$('#messages').on('click', function () {
console.log('messages');
$('#requests').popover('hide');
});
});
Why 'click'
? Because the default popover trigger for version 2.1.1 is click
. See the popover doc (github)
You can use the following events :
trigger: 'click'
: on click
trigger: 'hover'
: display on mouseenter
and hide on mouseleave
trigger: 'focus'
: display on focus
and hide on blur
trigger: 'manual'
: use your own code to display and hide anywayIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With