I Am Trying to make a popover with a bootstrap carousel and where the carousel items are to be generated and appended from a script but I get the carousel but I am unable to append the item. I tried hard but I didn’t come up with a solution.
the HTML initialized is as below
HTML:
JavaScript
x
12
12
1
<div class="popup" data-popup="popup-1">
2
<div class="popup-inner">
3
<a href="#" class="pop-head"><i class="fas fa-bars"></i></a>
4
<div class="frame">
5
<div id='carousel' class='carousel slide' data-bs-ride='carousel'>
6
<div class='carousel-inner items-slider1'>
7
</div>
8
</div>
9
</div>
10
</div>
11
</div>
12
the script I have tried was
Javascript:
JavaScript
1
38
38
1
function findallchord(currentchord , currenttype) {
2
for (let i = 1; i < 10; i++) {
3
if (Raphael.chord.find(currentchord ,currenttype,i)) {
4
Raphael.chord("div3", Raphael.chord.find(currentchord , currenttype,i), currentchord +' ' +currenttype).element.setSize(75, 75);
5
}
6
}
7
}
8
9
var getChordRoots = function (input) {
10
if (input.length > 1 && (input.charAt(1) == "b" || input.charAt(1) == "#"))
11
return input.substr(0, 2);
12
else
13
return input.substr(0, 1);
14
};
15
16
$('.popup').on('shown.bs.popover', function () {
17
$('.carousel-inner').carousel();
18
});
19
20
$('[data-bs-toggle="popover"]').popover({
21
html: true,
22
content: function() {
23
return $('.popup').html();
24
}}).click(function() {
25
var oldChord = jQuery(this).text();
26
var currentchord = getChordRoots(oldChord);
27
var currenttype = oldChord.substr(currentchord.length);
28
findallchord(currentchord , currenttype);
29
var chordsiblings = $('#div3').children().siblings("svg");
30
for (let i = 1; i < 10; i++) {
31
if (Raphael.chord.find(currentchord , currenttype,i)) {
32
var itemid = "chord" + i;
33
var theDiv = "<div class='carousel-item"+((itemid=="chord1") ? ' active':'')+" ' id='"+currentchord+''+itemid+"'> "+chordsiblings[i-1].outerHTML+" </div>";
34
$('.items-slider1').append(theDiv);
35
}
36
}
37
});
38
I have also tried appendTo also as
JavaScript
1
2
1
$(theDiv).appendTo('.items-slider1');
2
Please Help to solve this
This is the output I get, the appended elements are not in the carousel
Note: I am using Bootstrap 5
Advertisement
Answer
It was needed to do call the click function first before popover as below
JavaScript
1
21
21
1
$('[data-bs-toggle="popover"]').click(function() {
2
var oldChord = jQuery(this).text();
3
var currentchord = getChordRoots(oldChord);
4
var currenttype = oldChord.substr(currentchord.length);
5
findallchord(currentchord , currenttype);
6
var chordsiblings = $('#div3').children().siblings("svg");
7
for (let i = 1; i < 10; i++) {
8
if (Raphael.chord.find(currentchord , currenttype,i)) {
9
var itemid = "chord" + i;
10
var theDiv = "<div class='carousel-item"+((itemid=="chord1") ? ' active':'')+" ' id='"+currentchord+''+itemid+"'> "+chordsiblings[i-1].outerHTML+" </div>";
11
$('.items-slider1').append(theDiv);
12
}
13
}
14
}).popover({
15
html: true,
16
content: function() {
17
return $('.popup').html();
18
}});
19
20
21