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
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/
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.
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');
}
});
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');
});
});
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