Thursday, May 23, 2024
 Popular · Latest · Hot · Upcoming
129
rated 0 times [  136] [ 7]  / answers: 1 / hits: 34249  / 10 Years ago, thu, january 29, 2015, 12:00:00

I have a dynamic set of contenteditable divs. Divs that have class .showPopover, will have a popover. The popover trigger is set to manual, because I want them to appear on focus, but not always hide on blur.



I found here [question]: Bootstrap Tooltip with manual trigger and selector option
that I can't use the selector method together with the manual trigger, so I followed one of the answers there, but the popover still doesn't appear for dynamically added divs.



The problem is, that I only want popover to appear for divs with specific class, which is not added together with the div.



The change of div's class for the popover is a bit simplified with an enable button.



jQuery(document).ready(function($) {

$('a.add').on('click', function(event) {
event.preventDefault();
$('.container').append('<p class=input contenteditable=true></p>');
});

$('a.enable').on('click', function(event) {
event.preventDefault();
$('.input').not('.showPopover').addClass('showPopover');
});

$('.container').on('focus', '.input.showPopover', function(event) {
if (!$(this).data(bs.popover)) {
$(this).popover({
placement:'right',
trigger:'manual',
html:true,
content:'<a href=# class=btn btn-danger>Remove</a>'
});
}
$(this).popover('show');
});

var mousedownHappened = false;

$('.container').on('blur', '.input', function(event) {
if(mousedownHappened) {
mousedownHappened = false;
} else {
$(this).popover('hide');
}
});

$('.container').on('mousedown', '.popover .btn', function(event) {
mousedownHappened = true;
});

});


Jsfiddle: http://jsfiddle.net/Lh2rpj0f/2/



Jquery 1.11.1, Bootstrap 3.3.2






So thanks to Yenne Info, I've got a working solution:
http://jsfiddle.net/Lh2rpj0f/4/



It might not be the best solution, but it does exactly what I wanted. (When I click the button inside popover, this popover is not destroyed when Enable button is clicked.)






As for now, my final solution: Bootstrap popover with manual trigger attached on dynamic content


More From » jquery

 Answers
14

I updated my original code, and now it also works as I expected.



$('.container').on('focus', '.input.showPopover', function(event) {
if (!$(this).data(bs.popover) || !$(this).attr('data-popoverAttached')) {
$(this).popover('destroy').popover({
placement:'right',
trigger:'manual',
html:true,
content:'<a href=# class=btn btn-danger>Remove</a>'
});
$(this).attr('data-popoverAttached', true);
}
$(this).popover('show');
});


JSfiddle: http://jsfiddle.net/Lh2rpj0f/5/



But still, I think there is something wrong inside the bootstrap popover code. I think the reason why my original code doesn't work, is that the bootstrap popover is somehow magically attaching (with default options!) to all my dynamically added divs (even though they doesn't have class .showPopover). Because of that, when focus fires, it doesn't pass through the if statement. The data-popoverAttached attribute solves this problem.


[#68038] Wednesday, January 28, 2015, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
brandt

Total Points: 43
Total Questions: 90
Total Answers: 111

Location: Aruba
Member since Fri, Jun 24, 2022
2 Years ago
;