Izzy
Izzy

Reputation: 192

Bootstrap carousel - carousel.on is not a function

I'm using this standard Bootstrap carousel:

<div id="myCarousel" class="carousel slide" data-ride="carousel">
    <!-- Indicators -->
    <ol class="carousel-indicators">
        <li data-target="#myCarousel" data-slide-to="0" class="active"></li>
        <li data-target="#myCarousel" data-slide-to="1"></li>
        <li data-target="#myCarousel" data-slide-to="2"></li>
        <li data-target="#myCarousel" data-slide-to="3"></li>
    </ol>

    <!-- Wrapper for slides -->
    <div class="carousel-inner" role="listbox">
        <div class="item active">
            <div class="carousel-caption">
                <h1>App Preview</h1>
            </div>
        </div>

        <div class="item">
            <div class="carousel-caption">
                <h1>Contact Details</h1>
            </div>
        </div>

        <div class="item">
            <div class="carousel-caption">
                <h1>Choose plan</h1>
            </div>
        </div>

        <div class="item">
            4
        </div>
    </div>

    <!-- Left and right controls -->
    <a class="left carousel-control" href="#myCarousel" 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="#myCarousel" role="button" data-slide="next">
        @*<span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>*@
        <span class="sr-only">Next</span>
    </a>
</div>

and I'm trying to hide the 'previous' and 'next' buttons respectively, if the user is on the first or last items of the carousel:

$('.carousel').carousel.on('slid', '', function () {
    var $this = $(this);

    $this.children('.carousel-control').show();

    if ($('.carousel-inner .item:first').hasClass('active')) {
        $this.children('.left.carousel-control').hide();
    } else if ($('.carousel-inner .item:last').hasClass('active')) {
        $this.children('.right.carousel-control').hide();
    }

});

On the first line of the JavaScript above I get a 'carousel.on is not a function' error. JQuery is clearly working, and the carousel object is recognised. Here are the includes I'm using at the top of my ASP.NET MVC page that uses the carousel:

<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js" integrity="sha512-K1qjQ+NcF2TYO/eI3M6v8EiNYZfA95pQumfvcVrTHtwQVDG+aHRqLi/ETn2uB+1JqwYqVG3LIvdm9lj6imS/pQ==" crossorigin="anonymous"></script>

Upvotes: 0

Views: 1518

Answers (1)

aarjithn
aarjithn

Reputation: 1181

It should be

$('.carousel').on('slid', '', function () {
    ...
});

JSFiddle Demo

Upvotes: 4

Related Questions