How to make a Bootstrap Collapse button disable the other

Asked

Viewed 2,173 times

2

I created two buttons Collapse in Bootstrap with two different contents.

What happens:

When I click the first button the content expands. When I click the second button the second content expands below.

What I wanted:

I wanted when I clicked on the first button the content would open as it already does, but when I clicked on the second button the contents of the first one would close leaving only the second content.

<div class="container">
  <div class="row justify-content-md-center">
    <div class="col-sm-12 col-md-12 col-lg-3 col-xl-3 margem-baixa-5">
      <button type="button" class="btn btn-outline-primary btn-block btn-vert" data-toggle="collapse" href="#jan-cor-2f" aria-expanded="false">Abrir: 2 Folhas</button>
    </div>
    <div class="col-sm-12 col-md-12 col-lg-3 col-xl-3 margem-baixa-5">
      <button type="button" class="btn btn-outline-primary btn-block btn-vert" data-toggle="collapse" href="#jan-cor-3f" aria-expanded="false">Abrir: 3 Folhas</button>
    </div>
  </div>
</div>
<div class="collapse" id="jan-cor-2f">
  <div class="container">
    <div class="row" align="center">
      <?php  include"pages/ecommece/inc/jan-cor/2f-ven/jan-cor-2f-ven-1500x800.php"; ?>
      <?php  include"pages/ecommece/inc/jan-cor/2f-ven/jan-cor-2f-ven-1500x1000.php"; ?>

    </div>
  </div>
</div>
<div class="collapse" id="jan-cor-3f">
  <div class="container">
    <div class="row" align="center">
      <?php  include"pages/ecommece/inc/jan-cor/3f/jan-cor-3f-1500x800.php"; ?>
      <?php  include"pages/ecommece/inc/jan-cor/3f/jan-cor-3f-1500x1000.php"; ?>

    </div>
  </div>
</div>
</div>

2 answers

0

Bruno Rosa, no Bootstrap na documentação do 3, em Componentes tem sobre Collapse, onde tem a possibilidade de usar Accordion no seu caso, teste o código abaixo utilizando Accordion.

.panel {
  box-shadow: none !important;
}
<script type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">

<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

<div class="container">

  <a role="button" class="btn btn-default" data-toggle="collapse" data-parent="#accordion" href="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
    Jan 2F
  </a>
  <a class="btn btn-default" role="button" data-toggle="collapse" data-parent="#accordion" href="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
    Jan 3F
  </a>
  


  <div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
    <div class="panel">
      <div id="collapseOne" class="panel-collapse collapse in" role="tabpanel">
        <div>
          Include Jan 2F
        </div>
      </div>
    </div>
    <div class="panel">
      <div id="collapseTwo" class="panel-collapse collapse" role="tabpanel">
        <div>
          Include Jan 3F
        </div>
      </div>
    </div>
  </div>
</div>

https://getbootstrap.com/docs/3.3/javascript/#Collapse-example-accordion

In Bootstrap 4 changes only the Panel, from a look. https://getbootstrap.com/docs/4.1/components/collapse/#accordion-example

0

You can use jQuery for this:

$('#id-botao').click(function(){
  $('#div-esconder').collapse('hide');
});

I’ve already used this code in a project, it’s supposed to work...

  • but where I put it and how I do it, I’m new to php, and have no knowledge in jquery

  • @Brunorosa add a <script> tag on the footer of your page, and don’t forget to link to the Jquery library, here’s how to do it right: https://www.w3schools.com/jquery/jquery_get_started.asp

Browser other questions tagged

You are not signed in. Login or sign up in order to post.