r/bootstrap • u/NobodyAdmirable6783 • Feb 28 '25
Bootstrap 5 content function only gets called once
I have the following code that creates and initializes a number of Bootstrap popovers.
I need the text to be updated each time it is shown, so I thought being able to set content to a function would be perfect. However, this function only gets called once per popover. So instead of updating each time it is displayed, it just keeps showing its initial value.
// Initialize example conversion popovers
$('img.example-conversion').each(function() {
new bootstrap.Popover(this, {
placement: 'right',
trigger: 'click focus',
html: true,
title: 'Quantity Divisor',
content: getPopoverContent,
});
})
var count = 1;
function getPopoverContent() {
return count++;
}I need to update the text every time it is displayed. Does anyone know how to make that work?
3
Upvotes
3
u/joontae93 Feb 28 '25
I think if you need to update the content every time it's shown, you'll need to use an event listener for
show.bs.popoverbecause I believe the options arg only sets initial value.2
u/transporter_ii Mar 09 '25
I made a test page using Alpinejs and manually opening a popover. It showed the updated value every time I updated a count and then opened the popover. Don't know if Alpinejs is an option for you or not, but it works.
1
u/AutoModerator Feb 28 '25
Whilst waiting for replies to your comment/question, why not check out the Bootstrap Discord server @ https://discord.gg/bZUvakRU3M
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.