To get this to work you have to make sure to add a small delay
to the popover options, the default value is 0
. Which makes this for some reason impossible to work.
Delete the data-content
attribute from the link triggering the popover, you don't need that, and it will not work, even if you set the html
property in the options to true
.
The next step is to add an event-listener to the triggering element, which will listen for the inserted.bs.popover
event. This one gets fired when the template is added to the DOM.
At that point you can create an element, add it to the popover and assign your listener for click events to it. You do not have to create the element you could as well just use the data-content
attribute to add your element.
Here you can find information on Popover.js options and events
Update!
I just found out that it is actually only the delay that is needed. So the Solution mentioned by Parth Shah will work as well.
Looks like the hide event of the popover is being trigger before you can click the inside link.
So that is all that is needed
$('you-selector').popover({
delay: 100
});
$(document).ready(function(){
$('[data-toggle="popover"]').popover({
delay: 100 // this is definitely needed !
});
$('#testOutside').click(function(){
alert('Outside');
console.log('outside');
});
});
// Listen for inserted template to DOM
$('[data-toggle="popover"]').on('inserted.bs.popover', function () {
console.log($('.popover-content').find('#testInside'));
// Create the inside link.
$inside = $('<a href="#" id="inside">Inside</a>');
// Add the click event-handler only once
$inside.one('click', function() {
alert('Inside');
console.log('foo');
});
$('.popover-content').append($inside[0])
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/>
<a href='#' class='btn' title ='Test' data-trigger='focus' data-toggle='popover' data-html='true' id = 'testOutside'>Click Here </a>