Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using interactive elements inside a bootstrap popover

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
}
like image 923
Kalin C Ringkvist Avatar asked Jun 01 '12 20:06

Kalin C Ringkvist


People also ask

What is the difference between Tooltip and popover?

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.

How do I customize Bootstrap popover?

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.

How does Bootstrap popover work?

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.

How do I show popover on hover?

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" });


3 Answers

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.

like image 90
Alp Avatar answered Oct 12 '22 01:10

Alp


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.

like image 44
Kalin C Ringkvist Avatar answered Oct 11 '22 23:10

Kalin C Ringkvist


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>
like image 32
timborden Avatar answered Oct 12 '22 00:10

timborden