user2242044
user2242044

Reputation: 9253

Bootstrap collapse class doesn't toggle correctly

I am trying to use the Bootstrap collapse class. I have two issues. The first is when I click my collapse button initially, it takes a second click to collapse the div. Second, when it does collapse the text contained with the div is still visible. I can tell collapse is doing something because I added a border around the outer div.

JSFiddle demonstrating the issue: https://jsfiddle.net/sw52dytm/3/

<div style="border: 3px solid #A9A9A9">
  <button data-toggle="collapse"
          data-target="#Info_Collapse"
          class="btn btn-link collapsed">collapse</button>
  <h2>Info</h2>
  <div class="accordion-body collapse show" id="Info_Collapse">
    <span>test</span> 
  </div>
</div>

Upvotes: 2

Views: 3272

Answers (1)

Lakindu Gunasekara
Lakindu Gunasekara

Reputation: 4281

Try this snippet. I have changed some classes

h2 {
  margin: 0;
  display: inline-block;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/css/bootstrap.min.css" integrity="sha384-Zug+QiDoJOrZ5t4lssLdxGhVrurbmBWopoEl+M6BdEfwnCJZtKxi1KgxUyJq13dy" crossorigin="anonymous">

<div style="border: 3px solid #A9A9A9">
  <h2>Info</h2>
  <button class="btn btn-link collapsed" type="button" data-toggle="collapse" data-target="#collapseExample" aria-expanded="false" aria-controls="collapseExample">
    collapse
  </button>

  <div class="collapse" id="collapseExample">

    <span>test</span>
  </div>
</div>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/js/bootstrap.min.js" integrity="sha384-a5N7Y/aK3qNeh15eJKGWxsqtnX/wWdSZSKp+81YjTmS15nvnvxKHuzaWwXHDli+4" crossorigin="anonymous"></script>

EDITED:

Loading with expanding the content

Changed log: aria-expanded="true" and adding show class to

<div class="collapse show" id="collapseExample">

h2 {
  margin: 0;
  display: inline-block;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/css/bootstrap.min.css" integrity="sha384-Zug+QiDoJOrZ5t4lssLdxGhVrurbmBWopoEl+M6BdEfwnCJZtKxi1KgxUyJq13dy" crossorigin="anonymous">

<div style="border: 3px solid #A9A9A9">
  <h2>Info</h2>
  <button class="btn btn-link collapsed" type="button" data-toggle="collapse" data-target="#collapseExample" aria-expanded="true" aria-controls="collapseExample">
        collapse
      </button>

  <div class="collapse show" id="collapseExample">

    <span>test</span>
  </div>
</div>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/js/bootstrap.min.js" integrity="sha384-a5N7Y/aK3qNeh15eJKGWxsqtnX/wWdSZSKp+81YjTmS15nvnvxKHuzaWwXHDli+4" crossorigin="anonymous"></script>

For more information you can refer bootstrap 4 documentation. The relevant link is provided below. https://getbootstrap.com/docs/4.0/components/collapse/

Upvotes: 2

Related Questions