I’m trying to load a bootstrap 5 popover with dynamic content from an asynchronous function in which I have to wait for. I’m setting up my popover as so:
document.querySelectorAll('[data-bs-toggle="popover"]').forEach(function (popover) {
new bootstrap.Popover(popover, {
content: function () {
(async () => {
var x = await SetPopoverContent();
return x;
})()
}
});
});
I’m then going back to the database and retrieving my data inside SetPopoverContent():
async function SetPopoverContent(popOver) {
let contentText = '';
let data = await dotNetReference.invokeMethodAsync('GetChatToInfo', messageId);
if (data != null && data != undefined) {
var outerDiv = '<div></div>';
...
contentText = outerDiv.innerHTML;
}
else {
contentText = '<p>Loading</p>';
}
return contentText;
}
I can see my html string inside my popover content function but the content never appears in the popover. Am I doing something wrong with the async callback method?
2
Answers
What I think this should work.
It won’t work with async calls, you need to use
.setContent
method instead on popover instance (alternatively, you could create popover after async call, or set content toloading...
and then use.setContent
).So, get popover instance, then run your async method, and then assign new content:
demo:
see example for setting content: Popovers – Methods – setContent example