Elaine Byene Elaine Byene - 5 months ago 20
Javascript Question

Show multiple div when clicked in Bootstrap Carousel

DEMO JSFiddle

I'm trying to show multiple div when a button is clicked. For instance, I want to show all the divs with id=div1 only when Button1 is clicked. Likewise, show all the divs with id=div2 only when Button2 is clicked. The same goes for other slides too in increasing number.

HTML:

<div class="container">
<div id="myCarousel" class="carousel slide" data-ride="carousel">

<!-- Wrapper for slides -->
<div class="carousel-inner" role="listbox">
<div class="item active">
<div class="divA">
<div class="divAA">
<div id="div1" class="targetDiv">
<p>Div1 Intro: Hidden until Button1 is clicked</p>
</div>
</div>
<p>
Div1 Short to be shown by default.
<span id="div1" class="targetDiv">Hidden span until Button1 is clicked</span>
</p>
<a class="showSingle" target="1">Button1</a>
<div class="divAAA">
<div id="div1" class="targetDiv">
<p>
Div1 Details: Hidden until Button1 is clicked.
</p>
</div>
</div>
</div>
</div>

<div class="item">
<div class="divB">
<div class="divBB">
<div id="div2" class="targetDiv">
<p>Div2 Intro: Hidden until Button1 is clicked
<span id="div2" class="targetDiv">Hidden span until Button1 is clicked</span>
</p>
</div>
</div>
<p>
Div2 Short
</p>
<a class="showSingle" target="2">Button2</a>
<div id="div2" class="targetDiv">
<p>
Div2 Details: Hidden until Button2 is clicked. Div2 Details: Hidden until Button2 is clicked.Div2 Details: Hidden until Button2 is clicked.Div2 Details: Hidden until Button2 is clicked.Div2 Details: Hidden until Button2 is clicked.Div2 Details: Hidden until Button2 is clicked.Div2 Details: Hidden until Button2 is clicked.Div2 Details: Hidden until Button2 is clicked.Div2 Details: Hidden until Button2 is clicked.Div2 Details: Hidden until Button2 is clicked.Div2 Details: Hidden until Button2 is clicked.Div2 Details: Hidden until Button2 is clicked.Div2 Details: Hidden until Button2 is clicked.Div2 Details: Hidden until Button2 is clicked.Div2 Details: Hidden until Button2 is clicked.Div2 Details: Hidden until Button2 is clicked.Div2 Details: Hidden until Button2 is clicked.Div2 Details: Hidden until Button2 is clicked.Div2 Details: Hidden until Button2 is clicked.Div2 Details: Hidden until Button2 is clicked.Div2 Details: Hidden until Button2 is clicked.Div2 Details: Hidden until Button2 is clicked.Div2 Details: Hidden until Button2 is clicked. YOLO.
</p></div>
</div>
</div>

<div class="item">
3
</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>
</div>


Javascript:

jQuery(function(){
jQuery('.showSingle').click(function(){
jQuery('.targetDiv').hide();
jQuery('#div'+jQuery(this).attr('target')).show();
});
});

Answer

You cannot use same id for multiple elements. You need to change the id's to class.

You can hide them initially using CSS - display:none , Then on button click it can be showed using the class selector instead of id selector.

Here is the changed code.

In case you prefer a fiddle : https://jsfiddle.net/ga6gmy8m/2/

jQuery(function(){
        jQuery('.showSingle').click(function(){
              jQuery('.targetDiv').hide();
             jQuery(this).hide(); jQuery('.div'+jQuery(this).attr('target')).show();
        });
});
.carousel {
  height: 200px;
  overflow-x: hidden;
  overflow-y: scroll;
}
.item {
  text-align: center;
}
.div2,.div1{
  display:none;
}
.showSingle {
  border: 1px solid #000;
  padding: 5px 10px;
  cursor: pointer;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>

<div class="container">
  <div id="myCarousel" class="carousel slide" data-ride="carousel">

    <!-- Wrapper for slides -->
    <div class="carousel-inner" role="listbox">
      <div class="item active">
        <div class="divA">
          <div class="divAA">
            <div class="div1" class="targetDiv">
              <p>Div1 Intro: Hidden until Button1 is clicked</p>
            </div>
          </div>
          <p>
            Div1 Short to be shown by default.
            <span class="div1" class="targetDiv">Hidden span until Button1 is clicked</span>
          </p>
          <a class="showSingle" target="1">Button1</a>
          <div class="divAAA">
            <div class="div1" class="targetDiv">
              <p>
                Div1 Details: Hidden until Button1 is clicked.
              </p>
            </div>
          </div>
        </div>
      </div>

      <div class="item">
        <div class="divB">
          <div class="divBB">
            <div class="div2" class="targetDiv">
              <p>Div2 Intro: Hidden until Button1 is clicked
              <span class="div2" class="targetDiv">Hidden span until Button1 is clicked</span>
              </p>
            </div>
          </div>
          <p>
            Div2 Short
          </p>
          <a class="showSingle" target="2">Button2</a>
          <div class="div2" class="targetDiv" style="display:none">
            <p>
              Div2 Details: Hidden until Button2 is clicked. Div2 Details: Hidden until Button2 is clicked.Div2 Details: Hidden until Button2 is clicked.Div2 Details: Hidden until Button2 is clicked.Div2 Details: Hidden until Button2 is clicked.Div2 Details: Hidden until Button2 is clicked.Div2 Details: Hidden until Button2 is clicked.Div2 Details: Hidden until Button2 is clicked.Div2 Details: Hidden until Button2 is clicked.Div2 Details: Hidden until Button2 is clicked.Div2 Details: Hidden until Button2 is clicked.Div2 Details: Hidden until Button2 is clicked.Div2 Details: Hidden until Button2 is clicked.Div2 Details: Hidden until Button2 is clicked.Div2 Details: Hidden until Button2 is clicked.Div2 Details: Hidden until Button2 is clicked.Div2 Details: Hidden until Button2 is clicked.Div2 Details: Hidden until Button2 is clicked.Div2 Details: Hidden until Button2 is clicked.Div2 Details: Hidden until Button2 is clicked.Div2 Details: Hidden until Button2 is clicked.Div2 Details: Hidden until Button2 is clicked.Div2 Details: Hidden until Button2 is clicked. YOLO.
            </p></div>
        </div>
      </div>
    
      <div class="item">
        3
      </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>
</div>