Search code examples
javascriptjquerybootstrap-4show-hide

How to hide/show effect multiple on click in bootstrap 4 toggle


I have done the Hide and Show effects using on click

    $(document).ready(function () {
        // hiding clear button first
        $(".clearfiltershow .clearFiltersBtn").css('visibility', 'hidden');
        // on click showing when the class got triggered
        $(document).on('click', '.sortAndFilterButton.filtersButton.active', function () {
            $(".clearfiltershow .clearFiltersBtn").css('visibility', 'visible');
        });
        // on click hiding when the class got triggered
        $(document).on('click', '.sortAndFilterButton.filtersButton.collapsed', function () {
            $(".clearfiltershow .clearFiltersBtn").css('visibility', 'hidden');
        });

    });
   <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
    <div class="filterBy clearfiltershow">
        <a href="#" id="clearFiltersBtn" class="clearFiltersBtn">Clear</a>
        <button class="sortAndFilterButton filtersButton active" type="button" data-toggle="collapse"
            data-target="#filterPanel" id="filterButton" aria-expanded="true" aria-controls="filterPanel">
            Filters
        </button>
    </div>
    <div id="filterPanel">
        <p>hallo</p>
    </div>

jQuery but the effect is working for a single time and I want to show the effect whenever I click the respective button as per the script.

How can I achieve that?

Any help will be appreciated!


Solution

  • You can check if the button which is clicked has class active and not class collapsed depending on this show/hide your clear element.

    Demo Code :

    $(document).ready(function() {
      $(".clearfiltershow .clearFiltersBtn").css('visibility', 'hidden');
      $(document).on('click', '.sortAndFilterButton.filtersButton', function() {
        //check if the button has class active and not collapsed
        if ($(this).hasClass("active") && !$(this).hasClass("collapsed")) {
          $(".clearfiltershow .clearFiltersBtn").css('visibility', 'visible');
        } else {
          $(".clearfiltershow .clearFiltersBtn").css('visibility', 'hidden');
        }
      });
    
    });
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
    <div class="filterBy clearfiltershow">
      <a href="#" id="clearFiltersBtn" class="clearFiltersBtn">Clear</a>
      <button class="sortAndFilterButton filtersButton active" type="button" data-toggle="collapse" data-target="#filterPanel" id="filterButton" aria-expanded="true" aria-controls="filterPanel">
                Filters
            </button>
    </div>
    <div id="filterPanel">
      <p>hallo</p>
    </div>