I'm setting up the bootstrap carousel
after the Ajax call
to the DOM. So, I'm appending elements and classes after the page load
as below.
async function ajaxCall() {
let data = await fetch('ApiLink');
let jsonData = await data.json();
let carousel = document.createElement('section');
let inner = document.createElement('div');
carousel.id = 'slider';
carousel.className = 'carousel carousel-dark slide';
carousel.setAttribute('data-bs-ride', 'carousel');
inner.className = 'carousel-inner';
jsonData.data.forEach(data => {
let item = document.createElement('div');
let avatar = document.createElement('img');
item.className = 'carousel-item';
avatar.src = data.avatar;
item.appendChild(avatar);
inner.appendChild(item);
});
inner.querySelector('.carousel-item').classList.add('active');
carousel.appendChild(inner);
document.body.appendChild(carousel);
}
However, the carousel doesn't work.
I guess, I need to reinitialize the carousel after creating and appending the HTML elements. So, my question is:
How can I reinitialize the bootstrap carousel after the Ajax call without using jQuery?