JS:
$(function(){
$(\"#example\").popover({
placement: \'bottom\',
html: \'true\',
title : \'
Try this:
$(function(){
$("#example")
.popover({
title : 'tile',
content : 'test'
})
.on('shown', function(e){
var popover = $(this).data('popover'),
$tip = popover.tip();
var close = $('<button type="button" class="close" aria-label="Close"><span aria-hidden="true">×</span></button>')
.click(function(){
popover.hide();
});
$('.popover-title', $tip).append(close);
});
});
Rather than adding the button as a string of markup, it's much easier if you have a jQuery wrapped button because then you can bind much more neatly. This is indeed sadly missing from the Bootstrap code, but this workaround works for me, and actually could be expanded to apply to all popovers if desired.
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript" src="jquery.popover-1.1.2.js"></script>
<script type="text/javascript">
$(function(){
$("#example").popover({
placement: 'bottom',
html: 'true',
title : '<span class="text-info"><strong>title</strong></span> <button type="button" id="close" class="close">×</button></span>',
content : 'test'
})
$("#close").click(function(event) {
$("#example").popover('hide');
});
});
</script>
<button type="button" id="example" class="btn btn-primary" >click</button>
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="$("#example").popover("hide");">×</button>',
content : 'test'
});
});
Sticky on hover, HTML:
<a href="javascript:;" data-toggle="popover" data-content="Vivamus sagittis lacus vel augue laoreet rutrum faucibus." title="Lorem Ipsum">...</a>
Javascript:
$('[data-toggle=popover]').hover(function(e) {
if( !$(".popover").is(':visible') ) {
var el = this;
$(el).popover('show');
$(".popover > h3").append('<span class="close icon icon-remove"></span>')
.find('.close').on('click', function(e) {
e.preventDefault();
$(el).popover('hide');
}
);
}
});
I found the code below very useful (taken from https://www.emanueletessore.com/twitter-bootstrap-popover-add-close-button/):
$('[data-toggle="popover"]').popover({
title: function(){
return $(this).data('title')+'<span class="close" style="margin-top: -5px">×</span>';
}
}).on('shown.bs.popover', function(e){
var popover = $(this);
$(this).parent().find('div.popover .close').on('click', function(e){
popover.popover('hide');
});
});
I was running into the problem of the tooltip doing some funky stuff when the close button became clicked. To work around this I used a span
instead of using a button. Also, when the close button was clicked I would have to click the tooltip twice after it closed in order to get it to open again. To work around this I simply used the .click()
method, as seen below.
<span tabindex='0' class='tooltip-close close'>×</span>
$('#myTooltip').tooltip({
html: true,
title: "Hello From Tooltip",
trigger: 'click'
});
$("body").on("click", ".tooltip-close", function (e) {
else {
$('.tooltip').remove();
$('#postal-premium-tooltip').click();
}
});