user2823276
user2823276

Reputation: 195

bootstrap collapse: change display of toggle button icons and text

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>

Upvotes: 18

Views: 61929

Answers (5)

Paweł
Paweł

Reputation: 551

It is also possible to do by css styles.

in css add style:

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

and use in html:

<a class="btn btn-default text-toggle" 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-toggle

to link / button.

Upvotes: 55

prgmrDev
prgmrDev

Reputation: 914

If you're using jQuery then make use of toggleClass

$('div.toggler').click(function(){
  $('div.toggler span').toggleClass('glyphicon-chevron-down glyphicon-chevron-up');
});

Working JSFiddle here

Upvotes: 2

RASA
RASA

Reputation: 1

use this javascript for changing icon

$(document).ready(function(){
    $("#demo").on("hide.bs.collapse", function(){
        $(".btn").html('<span class="glyphicon glyphicon-collapse-down"></span> Open');
    });
    $("#demo").on("show.bs.collapse", function(){
        $(".btn").html('<span class="glyphicon glyphicon-collapse-up"></span> Close');
    });
    $("#demo1").on("hide.bs.collapse", function(){
        $(".btn1").html('<span class="glyphicon glyphicon-collapse-down"></span> Open');
    });
    $("#demo1").on("show.bs.collapse", function(){
        $(".btn1").html('<span class="glyphicon glyphicon-collapse-up"></span> Close');
    });
});

Upvotes: -1

May Allen
May Allen

Reputation: 9

I found this question and answer helpful. I removed the parent() method and added an id to my + button to avoid changing other buttons when toggling +.

$('#more').click(function () {
if($('button span').hasClass('glyphicon-chevron-down'))
{
    $('#more').html('<span class="glyphicon glyphicon-chevron-up"></span> Less Info'); 
}
else
{      
    $('#more').html('<span class="glyphicon glyphicon-chevron-down"></span> More Info'); 
}
}); 

http://jsfiddle.net/maybolles/opbyvbv4/2/

Upvotes: 0

Bass Jobsen
Bass Jobsen

Reputation: 49044

based on: https://stackoverflow.com/a/16870379/1596547

$('button span').parent().click(function () {
if($('button span').hasClass('glyphicon-chevron-down'))
{
   $('button').html('<span class="glyphicon glyphicon-chevron-up"></span> Close'); 
}
else
{      
    $('button').html('<span class="glyphicon glyphicon-chevron-down"></span> Open'); 
}
}); 

Upvotes: 8

Related Questions