2

Is there a way to have multiple generic bootstrap carousels without IDs on a page work separately? Currently, both carousels are working, but when I click prev/next then it changes both carousels. I would think this would target the carousel clicked?

Thanks!

HTML

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

        <!-- Wrapper for slides -->
        <div class="carousel-inner" role="listbox">
          <div class="item active">
            <img src="https://placeholdit.imgix.net/~text?txtsize=33&txt=960%C3%97370&w=960&h=370" alt="Image1">
          </div>

          <div class="item">
            <img src="https://placeholdit.imgix.net/~text?txtsize=33&txt=960%C3%97370&w=960&h=370" alt="Image2">
          </div>

          <div class="item">
            <img src="https://placeholdit.imgix.net/~text?txtsize=33&txt=960%C3%97370&w=960&h=370" alt="Image3">
          </div>

          <div class="item">
            <img src="https://placeholdit.imgix.net/~text?txtsize=33&txt=960%C3%97370&w=960&h=370" alt="Image4">
          </div>
        </div>

        <!-- Left and right controls -->
        <a class="left carousel-control" href=".carousel" 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=".carousel" role="button" data-slide="next">
          <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
          <span class="sr-only">Next</span>
        </a>
      </div>
      <!-- Second Carousel -->
      <div class="carousel slide" data-ride="carousel" data-wlp-component="wlp.carousel">
        <!-- Indicators -->
        <ol class="carousel-indicators">
          <li data-target=".carousel" data-slide-to="0" class="active"></li>
          <li data-target=".carousel" data-slide-to="1"></li>
          <li data-target=".carousel" data-slide-to="2"></li>
          <li data-target=".carousel" data-slide-to="3"></li>
        </ol>

        <!-- Wrapper for slides -->
        <div class="carousel-inner" role="listbox">
          <div class="item active">
            <img src="https://placeholdit.imgix.net/~text?txtsize=33&txt=960%C3%97370&w=960&h=370" alt="Image1">
          </div>

          <div class="item">
            <img src="https://placeholdit.imgix.net/~text?txtsize=33&txt=960%C3%97370&w=960&h=370" alt="Image2">
          </div>

          <div class="item">
            <img src="https://placeholdit.imgix.net/~text?txtsize=33&txt=960%C3%97370&w=960&h=370" alt="Image3">
          </div>

          <div class="item">
            <img src="https://placeholdit.imgix.net/~text?txtsize=33&txt=960%C3%97370&w=960&h=370" alt="Image4">
          </div>
        </div>

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

JS

        $('.carousel').each(function(){ 
            $(this).carousel({ 
                interval: 3000
             }); 
            $(this).on('click', '.left', function(){
                $(this).carousel('prev');
            });
            $(this).on('click', '.right', function(){
                $(this).carousel('next');
            });
        });
Fawn
  • 73
  • 1
  • 4
  • 20
  • Bootstrap will be attaching its `onclick` listeners to `.left` and `.right` by default (you can see them from inspector/developer's console), and I believe that's the reason your JS code does not work as expected. You could try to remove default handlers using jQuery's `.off()` method. – Kairat Jun 15 '16 at 04:30
  • @Kairat I see what you are saying. I'm curious if there is a way to find that specific parent .carousel – Fawn Jun 15 '16 at 14:04

1 Answers1

0

As per my understanding you need to have different selected to bind the events for 2 different carousel. So you need to use different class or different ID or any 2 different selected.