bootstrap collapse: change display of toggle button icons and text

user2823276 picture user2823276 · Sep 27, 2013 · Viewed 46.5k times · Source

I am using Twitter Bootstrap to create collapsible sections of text. The sections are expanded when a + button is pressed. My html code as follows:

Demo link

<div class="panel-group" id="accordion">
  <div class="panel panel-default">
    <div class="panel-heading">
      <h4 class="panel-title">
        <button type="button" class="btn btn-primary" data-toggle="collapse" data-target="#demo">
          <span class="glyphicon glyphicon-chevron-down"></span> Open
        </button>
      </h4>
    </div>
    <div id="demo" class="panel-collapse collapse in">
      <div class="panel-body">
        Contents:Thank you to help me solve the problem, you're a great guy!
      </div>
    </div>
  </div>
</div>

Later, i need to change text, button icons and expand.

Is Open:

<button type="button" class="btn btn-primary" data-toggle="collapse" data-target="#demo">
  <span class="glyphicon glyphicon-chevron-up"></span> Close
</button>

Is Close:

<button type="button" class="btn btn-primary" data-toggle="collapse" data-target="#demo">
  <span class="glyphicon glyphicon-chevron-down"></span> Open
</button>

Answer

Paweł picture Paweł · Feb 17, 2017

It is also possible to do by css styles.

in css add style:

.text-toogle[aria-expanded=false] .text-expanded {
  display: none;
}
.text-toogle[aria-expanded=true] .text-collapsed {
  display: none;
}

and use in html:

<a class="btn btn-default text-toogle" data-toggle="collapse" data-target="#tabsNavigation" aria-expanded="false">
    <span class="text-collapsed">More info</span>
    <span class="text-expanded">Less info</span>

    <i class="fa fa-angle-right"></i>
</a>

Please remember to add attribute

aria-expanded="false"

and class

text-toogle

to link / button.