Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap Collapse doesn't toggle after you show, hide or toggle from code

Tags:

My HTML is:

<div id="accordion-container">     <div class="accordion" id="navaccordion">         <div class="accordion-group">             <div class="accordion-heading">                 <a class="accordion-toggle" data-toggle="collapse" data-parent="#navaccordion" href="#collapseMenu">                     <strong>My Menus</strong>                 </a>             </div>             <div id="collapseMenu" class="accordion-body collapse in">                 <div class="accordion-inner">                     <div class="navigation" id="navigationcontainer">                         <span id="menutree">                             MenuTree                         </span>                     </div>                 </div>             </div>         </div>         <div class="accordion-group">             <div class="accordion-heading">                 <a class="accordion-toggle" data-toggle="collapse" data-parent="#navaccordion" href="#collapseQuickLinks">                     <strong>Quick Links</strong>                 </a>             </div>             <div id="collapseQuickLinks" class="accordion-body collapse">                 <div class="accordion-inner">                     <div class="quicklinks" id="quicklinkscontainer">                         <span id="quicklinkslist">                             QuickLinks                         </span>                     </div>                 </div>             </div>         </div>         <div class="accordion-group">             <div class="accordion-heading">                 <a class="accordion-toggle" data-toggle="collapse" data-parent="#navaccordion" href="#collapseQueue">                     <strong>Queue</strong>                 </a>             </div>             <div id="collapseQueue" class="accordion-body collapse">                 <div class="accordion-inner">                     <div class="queue" id="queuecontainer">                         <span id="queuetree">                             Queue                         </span>                     </div>                 </div>             </div>         </div>     </div> </div> 

My example is here: http://jsfiddle.net/pdavis68/Xut4C/2/

If you remove the JavaScript code, you'll notice that the toggling of the collapse operates properly. If you click "Quick Links", "My Menus" closes and "Quick Links" opens.

If you leave the JS in, you'll notice that opening "My Menus" or "Quick Links" doesn't cause anything else to collapse, but if you open "Queue", it will still cause the others to collapse.

It doesn't seem to matter if I use "toggle", "show" or "hide" command in the collapse, it will break the toggling functionality.

Also, in the example, what ought to happen (by my reckoning, at least) is that that "My Menus" should toggle closed (which it does) and then "Quick Links" ought to toggle open (which it does NOT do.)

So, 2 questions:

  1. How do I programmatically show/hide groups without breaking the toggle functionality?

  2. How do I toggle things open?

like image 400
Pete Avatar asked Jul 19 '13 16:07

Pete


People also ask

How do you toggle collapse in Bootstrap?

Just add data-toggle="collapse" and a data-target to the element to automatically assign control of one or more collapsible elements. The data-target attribute accepts a CSS selector to apply the collapse to. Be sure to add the class collapse to the collapsible element.

How does collapse work in Bootstrap?

How it works. The collapse JavaScript plugin is used to show and hide content. Buttons or anchors are used as triggers that are mapped to specific elements you toggle. Collapsing an element will animate the height from it's current value to 0 .

How do you implement Bootstrap collapse?

To control (show/hide) the collapsible content, add the data-toggle="collapse" attribute to an <a> or a <button> element. Then add the data-target="#id" attribute to connect the button with the collapsible content (<div id="demo">).

How do you keep multiple collapses open in Bootstrap 4?

Assuming that you're using Bootstrap 4, you can simply remove the data-parent attribute from the element with the collapse class. This subscribes the collapse to events on #accordionExample , which is the main accordion element, via the data-parent attribute.


2 Answers

1.Try to use collapse() with options, the 'parent' is important

$("#collapseMenu").collapse({"toggle": true, 'parent': '#navaccordion'}); $("#collapseQuickLinks").collapse({"toggle": true, 'parent': '#navaccordion' }); 

Fiddle: http://jsfiddle.net/hieuh25/Xut4C/6/

2.Basically you have 2 ways:

  • Add class in to that div, e.g: <div id="collapseMenu" class="accordion-body collapse in"> cause that div to open.

  • Use collapse() with option 'toggle': true as above, when the div is closed.

Hope it helps.

like image 200
Hieu Nguyen Avatar answered Sep 19 '22 00:09

Hieu Nguyen


Try activating your content as a collapsible element first. I skimmed over this part when reading the documentation and it really stumped me.

$('#myCollapsible').collapse({     toggle: false }) 

After you activate it you can hide and show it as usual.

$('#myCollapsible').collapse('hide'); $('#myCollapsible').collapse('show'); 

http://getbootstrap.com/javascript/#collapse-methods

like image 28
Jared Christensen Avatar answered Sep 21 '22 00:09

Jared Christensen