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.
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.
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.
Refer to Bootstrap Form Controls documentation to create inline checkboxes. Simply add data-toggle="toggle" to a convert checkboxes into toggles.
Just add data-toggle="collapse" and a data-target to the element to automatically assign control of one or more collapsible elements.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With