Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert close button in popover for Bootstrap

JS:

$(function(){   $("#example").popover({     placement: 'bottom',     html: 'true',     title : '<span class="text-info"><strong>title</strong></span> <button type="button" id="close" class="close">&times;</button>',     content : 'test'   })   $('html').click(function() {     $('#close').popover('hide');   }); }); 

HTML:

<button type="button" id="example" class="btn btn-primary" ></button> 

i'm write your code isn't show your popup.

anyone come across this problem?

like image 701
Danglebz Highbreed Avatar asked Nov 16 '12 08:11

Danglebz Highbreed


People also ask

How do I use Popovers in bootstrap?

To create a popover, add the data-toggle="popover" attribute to an element. Note: Popovers must be initialized with jQuery: select the specified element and call the popover() method.

How do I change the popover position in bootstrap?

Positioning with Margins So to move the popover more to the left, we can add a negative margin-left, or a positive one to move it further to the right. Likewise, we can move the popover more up by adding a negative margin-top, and down by using a positive value.

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


2 Answers

You need to make the markup right

<button type="button" id="example" class="btn btn-primary">example</button> 

Then, one way is to attach the close-handler inside the element itself, the following works :

$(document).ready(function() {     $("#example").popover({         placement: 'bottom',         html: 'true',         title : '<span class="text-info"><strong>title</strong></span>'+                 '<button type="button" id="close" class="close" onclick="$(&quot;#example&quot;).popover(&quot;hide&quot;);">&times;</button>',         content : 'test'     }); });   
like image 153
davidkonrad Avatar answered Sep 16 '22 21:09

davidkonrad


I needed to find something that would work for multiple popovers and in Bootstrap 3.

Here's what I did:

I had multiple elements I wanted to have a popover work for, so I didn't want to use ids.

The markup could be:

<button class="btn btn-link foo" type="button">Show Popover 1</button> <button class="btn btn-link foo" type="button">Show Popover 2</button> <button class="btn btn-link foo" type="button">Show Popover 3</button> 

The content for the save and close buttons inside the popover:

var contentHtml = [     '<div>',         '<button class="btn btn-link cancel">Cancel</button>',         '<button class="btn btn-primary save">Save</button>',     '</div>'].join('\n'); 

and the javascript for all 3 buttons:

This method works when the container is the default not attached to body.

$('.foo').popover({     title: 'Bar',     html: true,     content: contentHtml,     trigger: 'manual' }).on('shown.bs.popover', function () {     var $popup = $(this);     $(this).next('.popover').find('button.cancel').click(function (e) {         $popup.popover('hide');     });     $(this).next('.popover').find('button.save').click(function (e) {         $popup.popover('hide');     }); }); 

When the container is attached to 'body'

Then, use the aria-describedby to find the popup and hide it.

$('.foo').popover({     title: 'Bar',     html: true,     content: contentHtml,     container: 'body',     trigger: 'manual' }).on('shown.bs.popover', function (eventShown) {     var $popup = $('#' + $(eventShown.target).attr('aria-describedby'));     $popup.find('button.cancel').click(function (e) {         $popup.popover('hide');     });     $popup.find('button.save').click(function (e) {         $popup.popover('hide');     }); }); 
like image 40
Chris Avatar answered Sep 18 '22 21:09

Chris