Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twitter bootstrap collapse: change display of toggle button

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:

<div class="row-fluid summary">     <div class="span11">         <h2>MyHeading</h2>       </div>     <div class="span1">         <button type="button" class="btn btn-success" data-toggle="collapse" data-target="#intro">+</button>     </div> </div> <div class="row-fluid summary">     <div id="intro" class="collapse">          Here comes the text...     </div> </div> 

Is there a way to change the button to display - instead of + after the section is expanded (and change back to + when it is collapsed again)?

Additional information: I hoped there would be a simple twitter-bootstrap/css/html-based solution to my problem. All responses so far make use of JavaScript or PHP. Because of this I want to add some more information about my development environment: I want to use this solution inside a SilverStripe-based (version 3.0.5) website which has some implications for the use of both PHP as well as JavaScript.

like image 961
Markus M. Avatar asked Apr 25 '13 21:04

Markus M.


People also ask

How do I change the collapse button in Bootstrap?

Just add data-toggle="collapse" and a data-target to element to automatically assign control of a collapsible element. The data-target attribute accepts a CSS selector to apply the collapse to. Be sure to add the class collapse to the collapsible element. If you'd like it to default open, add the additional class in.

How do you expand and collapse accordion on button click?

By default, accordion items expand or collapse by clicking the accordion item header or clicking expand/collapse icon in accordion header. You can also expand or collapse the accordion items through external button click.

How do I toggle in Bootstrap?

Refer to Bootstrap Form Controls documentation to create inline checkboxes. Simply add data-toggle="toggle" to a convert checkboxes into toggles.

How do I make accordion closed by default Bootstrap?

Just add data-toggle="collapse" and a data-target to the element to automatically assign control of one or more collapsible elements.


1 Answers

try this. http://jsfiddle.net/fVpkm/

Html:-

<div class="row-fluid summary">     <div class="span11">         <h2>MyHeading</h2>       </div>     <div class="span1">         <button class="btn btn-success" data-toggle="collapse" data-target="#intro">+</button>     </div> </div> <div class="row-fluid summary">     <div id="intro" class="collapse">          Here comes the text...     </div> </div> 

JS:-

$('button').click(function(){ //you can give id or class name here for $('button')     $(this).text(function(i,old){         return old=='+' ?  '-' : '+';     }); }); 

Update With pure Css, pseudo elements

http://jsfiddle.net/r4Bdz/

Supported Browsers

button.btn.collapsed:before {     content:'+' ;     display:block;     width:15px; } button.btn:before {     content:'-' ;     display:block;     width:15px; } 

Update 2 With pure Javascript

http://jsfiddle.net/WteTy/

function handleClick() {     this.value = (this.value == '+' ? '-' : '+'); } document.getElementById('collapsible').onclick=handleClick; 
like image 159
PSL Avatar answered Sep 30 '22 18:09

PSL