0

I use bootstrap 4 alpha 6 version. I have several blocks in my page. I want expand/collapse these blocks by clicking main button (id='expand-collapse'). Also every button has there own individual buttons which would open/close concrete block. Right know I use next js code and have strange behavior.

For example: I open first block by clicking first button, then I click main button (id='expand-collapse') to open other blocks. But in fact first block closed and other blocks opened. How to fix this problem?

HTML:

<div class="card">
   <div class="card-header">
      <button id='expand-collapse' type="button" data-parent="#blocks"  data-toggle="collapse" data-target=".block" aria-expanded="false" aria-controls=".block">
      </button>
   </div>

   <div class="card-block">
       <div id="blocks">
          <div class="list-group">

             <div class="list-group-item">
                <a data-toggle="collapse" href="#block-1" aria-expanded="false" aria-controls="block-1">OPEN/CLOSE FIRST</a>
                <div class="collapse block" id="block-1">
                   FIRST BLOCK BLOCK-->
                </div>
             </div>

             <div class="list-group-item">
                <a data-toggle="collapse" href="#block-2" aria-expanded="false" aria-controls="block-2">OPEN/CLOSE SECOND</a>
                <div class="collapse block" id="block-2">
                   SECOND BLOCK
                </div>
            </div>

            <div class="list-group-item">
                <a data-toggle="collapse" href="#block-3" aria-expanded="false" aria-controls="block-3">OPEN/CLOSE THIRD</a>
                <div class="collapse block" id="block-3">
                   THIRD BLOCK
                </div>
            </div>

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

JAVASCRIPT:

$(function() {
  $('#expand-collapse').on('click', function() {
    var target = $(this).attr('data-target');
    $(target).each(function() {
      if ($(this).hasClass('show')) {
        $(this).collapse('hide');
      } else {
        $(this).collapse('show');
      }

    });
  });
});
Nurzhan Nogerbek
  • 4,806
  • 16
  • 87
  • 193

1 Answers1

0

That kind of behaviour is due to data-toggle attribute. Take that off from the main button and change the script to this- HTML

<button id='expand-collapse' type="button" data-parent="#blocks"   data-target=".block" aria-expanded="false" aria-controls=".block">
       </button>

JQUERY:

             $(function() {
             var showAll = false;
            $('#expand-collapse').on('click', function() {
              var target = $(this).attr('data-target');
              //console.log('showAll=' + showAll);
              $(target).each(function() {                     
                  if(showAll === false)  {
                        $(this).collapse('show');
                  }
                  else {
                    $(this).collapse('hide');
                  }
              });

              if(showAll === false) {
                  showAll = true;
              }
              else {
                  showAll = false;
              }

              //console.log('showAll=' + showAll);
            });
          });
T.Shah
  • 2,768
  • 1
  • 14
  • 13