I have an issue with my Bootstrap 5 modal and the popovers I’ve placed on to it.
In my HTML I have the following, which correctly displays when the user hovers over the button. The popover shows an ‘Are you sure?’ type prompt and advises the user to click the button (that is hovered) to continue. Clicking the button then takes the focus and keeps the popover open until clicked again, or clicked away from the element.
This allows the mouse pointer to enter the popover content and click the ‘Continue’ button.
This is the kind of thing I was going for: https://bootstrap-confirmation.js.org but with some more detail about the function shown when the button is hovered…
Anyway, it works great! First time around… If the modal is hidden and then shown again, only the hover part of the popover trigger works, and clicking the button does fire it’s listener, but the popover refuses to stay open (as per the ‘focus’ trigger)..
Please help, it’s driving me mad!
Frustratingly, the snippet below does seem to work, although will throw an error and I’m not sure why?
html
<!-- Close Session button -->
<a tabindex="0" role="button" class="btn btn-sm btn-danger" id="locate-btn-close" data-bs-trigger="hover focus" data-bs-theme="dark" data-bs-placement="top" data-bs-toggle="popover">Close Session</a>
<div id="popover-close-btn-holder" class="d-none">
<div data-name="close-btn-popover-content">
<div class="col-sm-12 input-group-sm">
<p>You can choose to close or save this session at the next screen.</p>
<p class="continue-text">Click the button to continue...</p>
</div>
<div class="text-center confirm-buttons" style="display:none;">
<button href="#" class="btn btn-sm btn-outline-danger me-2" id="locate-btn-close-cancel">Cancel</button>
<button href="#" class="btn btn-sm btn-outline-success" id="locate-btn-close-confirm">Continue</button>
</div>
</div>
</div>
JS:
const locateModal = document.getElementById('locateScreen')
locateModal.addEventListener('shown.bs.modal', event => {
//Close Session Button
// 1 - Init the popover
const locateBtnClosePopover = new bootstrap.Popover($('#locate-btn-close'), {
html: true,
sanitize: false,
customClass: 'locate-footer-popover',
title: "Are you sure?",
// trigger: 'hover focus', // Didn't help, already set in HTML
const locateModal = document.getElementById('locateScreen')
locateModal.addEventListener('shown.bs.modal', event => {
const locateBtnClosePopover = new bootstrap.Popover($('#locate-btn-close'), {
html: true,
sanitize: false,
customClass: 'locate-footer-popover',
title: "Are you sure?",
// trigger: 'focus',
content: $('[data-name="close-btn-popover-content"]')
});
// 2 - Listen for clicks on the 'Close' button
$('#locate-btn-close').off('click');
$('#locate-btn-close').on('click', function() {
// This is firing, but the popover is closing when the mouse moves towards
// the buttons, after the modal is shown once, hidden, then re-displayed.
// Show popover buttons in popover
$('[data-name="close-btn-popover-content"] .confirm-buttons').show();
// Assign listeners to the popover confirm buttons
//$('#locate-btn-close-confirm').off()
$('#locate-btn-close-confirm').on('click', function() { // Close Session screen
// Do close the session
});
// Hide 'Click to continue' text
$('[data-name="close-btn-popover-content"] .input-group-sm .continue-text').hide()
});
// When the popover is initially shown, hide the buttons and make sure the text reads:
// 'click the button to continue', etc
$('#locate-btn-close').on('inserted.bs.popover', function() {
$('[data-name="close-btn-popover-content"] .confirm-buttons').hide()
$('[data-name="close-btn-popover-content"] .input-group-sm .continue-text').show()
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-kenU1KFdBIe4zVF0s0G1M5b4hcpxyD9F7jL+jjXkk+Q2h455rYXK/7HAuoJl+0I4" crossorigin="anonymous"></script>
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#locateScreen">
Launch demo modal
</button>
<!-- Modal -->
<div class="modal fade" id="locateScreen" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h1 class="modal-title fs-5" id="exampleModalLabel">Modal title</h1>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
Popover is on the Save Changes button.
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<a id="locate-btn-close" tabindex="0" class="btn btn-danger" role="button" data-bs-toggle="popover" data-bs-trigger="hover focus" data-bs-title="Dismissible popover" data-bs-content="And here's some amazing content. It's very engaging. Right?">Save Changes</a>
<!-- popover -->
<div id="popover-close-btn-holder" class="d-none">
<div data-name="close-btn-popover-content">
<div class="col-sm-12 input-group-sm">
<p class="continue-text">Click the button to continue...</p>
</div>
<div class="text-center confirm-buttons" style="display:none;">
<button href="#" class="btn btn-sm btn-outline-danger me-2" id="locate-btn-close-cancel">Cancel</button>
<button href="#" class="btn btn-sm btn-outline-success" id="locate-btn-close-confirm">Continue</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
content: $('[data-name="close-btn-popover-content"]')
});
// 2 - Listen for clicks on the 'Close' button
$('#locate-btn-close').off('click');
$('#locate-btn-close').on('click', function() {
console.log('Close button clicked!')
// This is firing, but the popover is closing when the mouse moves towards
// the buttons, after the modal is shown once, hidden, then re-displayed.
// Show popover buttons in popover
$('[data-name="close-btn-popover-content"] .confirm-buttons').show();
// Assign listeners to the popover confirm buttons
//$('#locate-btn-close-confirm').off()
$('#locate-btn-close-confirm').on('click', function() { // Close Session screen
locateScreen.hide();
closeSession();
});
// Hide 'Click to continue' text
$('[data-name="close-btn-popover-content"] .input-group-sm .continue-text').hide()
});
// When the popover is initially shown, hide the buttons and make sure the text reads:
// 'click the button to continue', etc
$('#locate-btn-close').on('inserted.bs.popover', function() {
$('[data-name="close-btn-popover-content"] .confirm-buttons').hide()
$('[data-name="close-btn-popover-content"] .input-group-sm .continue-text').show()
});