I am curious to know how to create a dynamic Bootstrap 5 carousel. Specifically, I want to know how to display indicators dynamically and add an active class to the first item of the carousel.
In Bootstrap 4, it is done in the following way:
$('#main_carousel .carousel-item:first').addClass('active');
var myCarousel = $("#main_carousel");
myCarousel.append("<ol class='carousel-indicators d-none d-xl-flex'></ol>");
var indicators = $("#main_carousel .carousel-indicators");
myCarousel.find(".carousel-inner").children(".carousel-item").each(function(index) {
(index === 0) ?
indicators.append("<li data-target='#main_carousel' data-slide-to='"+index+"' class='active'></li>") :
indicators.append("<li data-target='#main_carousel' data-slide-to='"+index+"'></li>");
});
Could someone please provide instructions on how to achieve this using vanilla JavaScript for Bootstrap 5?
Thank you in advance!