1

Only when the button #collapse_init is clicked i manage to change the text on the button to "show all" or "hide all" according to the active Boolean variable

When I hide/collapse the tabs manually (one by one), the text on the button is not changed, i am trying to figure-up how to fix that.

  • i mean, when all tabs are shown or not (but not through the button) - the button text is not changed.

** currently the pen is not working correctly- the button is not working correctly(only in the "pen", in my code it's working), it's not collapse/hide all in once (trying to figure why, also).

var active = false; $('#collapse_init').click(function () { if (active) { active = false; $('.panel-collapse').collapse('hide'); $('.panel-title').attr('data-toggle', 'collapse'); $(this).text('Show all'); } else { active = true; $('.panel-collapse').collapse('show'); $('.panel-title').attr('data-toggle', ''); $(this).text('Hide all'); } }); $('.accordion-toggle').click(function () { var visible = $('.accordion-toggle:visible').length; if(visible >0){ $('#collapse_init').text('Hide all'); }else{ $('#collapse_init').text('Show all'); } });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link href="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet"/> <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script> <div class="container"> <button id="collapse_init" class="btn btn-default ">Show all</button>	<hr> <div class="panel-group" id="accordion"> <div class="panel panel-default"> <div class="panel-heading"> <h4 class="panel-title"> <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion" href="#collapseOne"> Collapsible Group Item #1 </a> </h4> </div> <div id="collapseOne" class="panel-collapse collapse "> <div class="panel-body"> ONe Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS. </div> </div> </div> <div class="panel panel-default"> <div class="panel-heading"> <h4 class="panel-title"> <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion" href="#collapseTwo"> Collapsible Group Item #2 </a> </h4> </div> <div id="collapseTwo" class="panel-collapse collapse "> <div class="panel-body"> Two Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS. </div> </div> </div> <div class="panel panel-default"> <div class="panel-heading"> <h4 class="panel-title"> <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion" href="#collapseThree"> Collapsible Group Item #3 </a> </h4> </div> <div id="collapseThree" class="panel-collapse collapse "> <div class="panel-body"> Three Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS. </div> </div> </div> </div> </div>

CodePen

codepen

2 Answers 2

2

Add this code and try

$('.accordion-toggle').click(function () { var visible = $('.accordion-toggle:visible').length; if(visible >0){ $('#collapse_init').text('Hide all'); }else{ $('#collapse_init').text('Show all'); } }); 
Sign up to request clarification or add additional context in comments.

3 Comments

I updated the pen with the code you suggested, its not working. try to show each tab (manually, one by one) the text on the button is not changed to "show all"
accordian is not working correctly in codepen. may be you can try in your code
or try with var visible = $('.collapse.in:visible').length;
1

I think the issue you're experiencing is that $('.accordion-toggle:visible').length is being retrieved before the panel has opened/closed.

You should use shown.bs.collapse and hidden.bs.collapse to detect when a panel has finished opening or closing.

Here's my crack. It's not quite there, but it's a fair bit cleaner (maybe).

var active = false, $collapseBtn = $('#collapse_init'), $accordion = $('#accordion'); $collapseBtn.on('click',function(){ active = ! active; $('.panel-collapse').collapse( active ? 'show' : 'hide'); $('.panel-title').attr('data-toggle', active ? '' : 'collapse' ); }) $accordion.on({ 'shown.bs.collapse': function () { active = true; $collapseBtn.text( 'Hide all' ); }, 'hidden.bs.collapse': function ( e) { active = ! ! $('.panel-collapse:visible').length; $collapseBtn.text( ( active ? 'Hide' : 'Show' ) + ' all' ); } }); 

https://codepen.io/jamespoel/pen/dRePNQ

6 Comments

what are tese classes mean? hidden.bs.collapse and shown.bs.collapse they are buit in what boostrap collapse?
also, what is ! ! in hidden.bs.collapse function
They're not classes, they're events. They're called internally by bootstrap. shown.bs.collapse Occurs when the collapsible element is fully shown hidden.bs.collapse Occurs when the collapsible element is fully hidden The double-bang (! !) in front of the $('.panel-collapse:visible').length is just a trick to quickly cast an integer to a boolean. It's basically the same as $('.panel-collapse:visible').length > 0; I just prefer it :) More on that here
the code is working (i have changed the code a bit), i encountered a bug > "manually" hide 2 tabs > click the button "show all" > only one of them is showing (the second one still in hidden state). there is a bug in that area , when you show/hide only one tab or more and then try to show/hide them all. How can i solve it?
I'm not exactly sure why, but removing the data-parent="#accordion" from each panel solved this issue. I updated my codepen. :-)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.