I am successfully using a bootstrap popover on a link click. I have some form elements inside the popover: a textfield, checkbox and a button. I'm able to attach a button listener using jquery.live(), but inside that button listener I don't seem to have any access to the correct form elements. They exist if I trace them out in a console log, but their values always remain the original defaults, so if I go $('#txtComment').val(); the string is always the same, regardless of what I put into the field.
Are there any examples, tutorials or source code I could look at of something that is using any kind of interactivity inside a bootstrap popover?
this is how I'm setting up the popover:
this.commentLink.popover({
trigger: 'manual',
placement: 'right',
html : true,
content: function () {
return $('#commentPopout').html();
}
}).popover('hide');
//jquery.on won't work here so we use live
$('#btnSubmitComment').live('click', this.proxy(this.commentSubmitClick));
then I'm doing this to successfully show it:
this.commentLink.popover('show');
and this is the button click function:
commentSubmitClick: function(e){
console.log($('#txtComment').val());//always shows default text regardless of what I've actually typed in the field
}
Tooltip: use tooltips to show a short text to respondents when they hover over a word or icon. Popover: use popovers to show a longer text, or when you want to have a link to an external web page. It is shown when the respondent clicks on a word or icon.
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.
Set the trigger option of the popover to hover instead of click , which is the default one. Or with an initialization option: $("#popover"). popover({ trigger: "hover" });
The syntax changed. The answer of Kalin C Ringkvist needs to be slightly modified:
var popover = this.commentLink.data('popover').tip();
Note the method tip()
instead of $tip
.
Yay! figured it out. Why, oh why is this not documented? I miss working in actionscript.
var popover = this.commentLink.data('popover').$tip;
var comment = popover.find('#txtComment').val();//gives correct string
This code must be run while the popover is actually visible, since the elements don't exist when it's not visible.
-Oh, and using jquery.live() is deprecated, but you can use this $tip variable to get the button reference instead once the popover is created.
For a variable number of buttons/links that have events binded, here's how I did it (going off what's been answered previously):
var list = $('<ul/>');
myCollections.items.each(function(item){
var row = $('<li/>');
row.append(item.get('id')));
row.append($('<a/>').addClass('delete').html('remove'));
list.append(row);
});
$(this.el).find('a').popover({ content: list }).on('click', function(){
var popover = $(this).data('popover').tip();
$(popover).find('.delete').on('click', removeitem);
});
function removeitem(){ // code to remove the item here }
// The HTML (in the popover) would look like this:
<ul>
<li>1<a class="delete">remove</a></li>
<li>2<a class="delete">remove</a></li>
<li>3<a class="delete">remove</a></li>
</ul>
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