Somea Furos
Somea Furos

Reputation: 53

Dynamically adding photographs to bootstrap carousel

I have the problem in my code. I would like to dynamicaly add images to bootstrap carousel. First problem that I encounter is that all images are set to active. And second is that left and right navigation is not working. Thanks for answer.

Ajax callback:

  success: function (data) {
            $(data.ResponseData.Data.fileNames).each(function (index, imageName) {

                $("<div class='item active'><img src='/Images/IndexSlideShow/" + imageName + "' alt='Graduation Image'></div>").appendTo("#indexCarousel .carousel-inner");
                $("<li data-target='#indexCarousel' data-slide-to='" + index + "'></li>").appendTo('#indexCarousel .carousel-indicators');

                $('#indexCarousel .item').first().addClass('active');
                $('#indexCarousel .carousel-indicators > li').first().addClass('active');
                $('#indexCarousel').carousel();
            });
        },

This is HTML code:

<div id="indexCarousel" class="carousel slide" data-ride="carousel">
                <!-- Indicators -->
                <ol class="carousel-indicators">
                </ol>

                <!-- Wrapper for slides -->
                <div class="carousel-inner" role="listbox">
                </div>

                <!-- Left and right controls -->
                <a class="left carousel-control" href="#indexCarousel" role="button" data-slide="prev">
                    <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
                    <span class="sr-only">Previous</span>
                </a>
                <a class="right carousel-control" href="#indexCarousel" role="button" data-slide="next">
                    <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
                    <span class="sr-only">Next</span>
                </a>
            </div>

Edit:

Upvotes: 2

Views: 3034

Answers (1)

Somea Furos
Somea Furos

Reputation: 53

<div class='item active'>

I was adding all items as active, this was the problem. It was obviously my mistake.

Upvotes: 3

Related Questions