Search code examples
jquerytwitter-bootstrapaccordion

Bootstrap 3 Collapse in other section one per time


I have put together a FAQ with categories. My idea is to use the function Collapse bootstrap to display each category and into the collapse see a accordion with questions and answers.

It is structured in two columns on the left select the category and the right questions and answers.

I need to select a second category (collapse) the previous close.

I could build up here.

<div class="container">
  <section id="tratamientos" class="row">
    <h1>Tratamientos</h1>
    <div class="col-sm-2">
      <button class="btn btn-default" type="button" data-toggle="collapse" data-target="#cirugiaoral" aria-expanded="false" aria-controls="cirugiaoral">Cirugía Oral</button>
      <br><br>
      <button class="btn btn-default" type="button" data-toggle="collapse" data-target="#blanqueamiento" aria-expanded="false" aria-controls="blanqueamiento">Blanqueamiento</button>
    </div>
    
    <div class="col-sm-10">
      
      <div class="collapse" id="cirugiaoral">
        <h3>Cirugía Oral</h3>
        /* FAQ with accordion */
        <ul>
          <li>item with accordion</li>
          <li>item with accordion</li>
        </ul>
      </div>
      
      <div class="collapse" id="blanqueamiento">
        <h3>Blanqueamiento</h3>
        /* FAQ with accordion */
        <ul>
          <li>item with accordion</li>
          <li>item with accordion</li>
        </ul>
      </div>
    </div>
  </section>
</div>

Bootply here


Solution

  • Add this script:

    $('.collapse').on('show.bs.collapse', function (e) {
        $('.collapse').not(e.target).removeClass('in');
    });
    

    That'll hide the other sections for you when a scection is shown

    See this Bootply