I'm using the popover feature from Twitter's Bootstrap js. I have a button that, when clicked, executes this javascript:
$("#popover_anchor").popover({trigger: "manual",
placement: "below",
offset: 10,
html: true,
title: function(){return "TITLE";},
content: function(){return "CONTENT TEXT";}});
$("#popover_anchor").popover("show");
There's also another button that executes basically the same javascript, except that the title and content functions return different text.
Note that they both define the popover on the same element, just with different content.
The problem is, once either button is clicked and the js is executed, no subsequent clicks from the other button will change the popover content. So once the popover is initialized, how can I update/change the content?
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.
The Popover plugin is similar to tooltips; it is a pop-up box that appears when the user clicks on an element. The difference is that the popover can contain much more content. Tip: Plugins can be included individually (using Bootstrap's individual "popover. js" file), or all at once (using "bootstrap.
You must include popper. min. js before bootstrap. js or use bootstrap.
the purpose of the title
attribute is to accept a value of type function
which provides this very functionality.
for example: if you set your title to:
title: function() { return randomTxt(); }
and you have,
function randomTxt()
{
arr = ['blah blah', 'meh', 'another one'];
return arr[Math.floor(Math.random() * 4)];
}
you will keep getting a different title for your popover. It is upto you to change the logic of randomText
to fetch a title dynamically.
If 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