Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>All the other solutions posted here cause the toggle to get out of sync if it is double clicked. The following solution uses the <a href="http://getbootstrap.com/2.3.2/javascript.html#collapse" rel="noreferrer">events provided by the Bootstrap framework</a>, and the toggle always matches the state of the collapsible element:</p> <p><strong>HTML:</strong></p> <pre><code>&lt;div class="row-fluid summary"&gt; &lt;div class="span11"&gt; &lt;h2&gt;MyHeading&lt;/h2&gt; &lt;/div&gt; &lt;div class="span1"&gt; &lt;button id="intro-switch" class="btn btn-success" data-toggle="collapse" data-target="#intro"&gt;+&lt;/button&gt; &lt;/div&gt; &lt;/div&gt; &lt;div class="row-fluid summary"&gt; &lt;div id="intro" class="collapse"&gt; Here comes the text... &lt;/div&gt; &lt;/div&gt; </code></pre> <p><strong>JS:</strong></p> <pre><code>$('#intro').on('show', function() { $('#intro-switch').html('-') }) $('#intro').on('hide', function() { $('#intro-switch').html('+') }) </code></pre> <p>That should work for most cases. </p> <p>However, I also ran into an additional problem when trying to nest one collapsible element and its toggle switch inside another collapsible element. With the above code, when I click the nested toggle to hide the nested collapsible element, the toggle for the parent element also changes. It may be a bug in Bootstrap. I found a solution that seems to work: I added a "collapsed" class to the toggle switches (Bootstrap adds this when the collapsible element is hidden but they don't start out with it), then added that to the jQuery selector for the hide function:</p> <p><a href="http://jsfiddle.net/fVpkm/87/" rel="noreferrer">http://jsfiddle.net/fVpkm/87/</a></p> <p><strong>HTML:</strong></p> <pre><code>&lt;div class="row-fluid summary"&gt; &lt;div class="span11"&gt; &lt;h2&gt;MyHeading&lt;/h2&gt; &lt;/div&gt; &lt;div class="span1"&gt; &lt;button id="intro-switch" class="btn btn-success collapsed" data-toggle="collapse" data-target="#intro"&gt;+&lt;/button&gt; &lt;/div&gt; &lt;/div&gt; &lt;div class="row-fluid summary"&gt; &lt;div id="intro" class="collapse"&gt; Here comes the text...&lt;br&gt; &lt;a id="details-switch" class="collapsed" data-toggle="collapse" href="#details"&gt;Show details&lt;/a&gt; &lt;div id="details" class="collapse"&gt; More details... &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; </code></pre> <p><strong>JS:</strong></p> <pre><code>$('#intro').on('show', function() { $('#intro-switch').html('-') }) $('#intro').on('hide', function() { $('#intro-switch.collapsed').html('+') }) $('#details').on('show', function() { $('#details-switch').html('Hide details') }) $('#details').on('hide', function() { $('#details-switch.collapsed').html('Show details') }) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload