Skip to content
Advertisement

How to change the collapse menu icon with JS?

I have a main menu (collapse) that uses Bootstrap 3.3.7 and Font Awesome 5.0.1

What I am looking for :

  • When the menu is closed, a “plus” icon is displayed.

  • When the menu is open, a “minus” icon is displayed.

The “plus” icon is displayed on the menu but does not change.

I think there is a problem with my JS code.

<nav role="navigation" aria-labelledby="block-navigationprincipale-menu" id="block-navigationprincipale">
   <ul class="menu nav navbar-nav">
      <li class="expanded dropdown open">
         <a href="/boutique" class="dropdown-toggle collapse-change-icon" data-toggle="dropdown"><i class="fas fa-plus-circle fa-lg"></i> Boutiques</a>
            <ul class="menu dropdown-menu">
               <li>
                  <a href="/boutique" data-drupal-link-system-path="boutique"><i class="fas fa-shopping-bag fa-lg"></i> Boutiques</a>
               </li>
               <li>
                  <a href="/produit" data-drupal-link-system-path="produit"><i class="fas fa-gift fa-lg"></i> Produits</a>
               </li>
               <li>
                  <a href="/service" data-drupal-link-system-path="service"><i class="fas fa-sign-language fa-lg"></i> Services</a>
               </li>
            </ul>
      </li>
      <li class="expanded dropdown">
         <a href="/groupe" class="dropdown-toggle collapse-change-icon" data-toggle="dropdown"><i class="fas fa-plus-circle fa-lg"></i> Groupes</a>
            <ul class="menu dropdown-menu">
               <li>
                  <a href="/groupe" data-drupal-link-system-path="groupe"><i class="fas fa-users fa-lg"></i> Groupes</a>
               </li>
               <li>
                  <a href="/annonce" data-drupal-link-system-path="annonce"><i class="fas fa-newspaper fa-lg"></i> Annonces</a>
               </li>
               <li>
                  <a href="/article" data-drupal-link-system-path="article"><i class="fas fa-file-alt fa-lg"></i> Articles</a>
               </li>
            </ul>
      </li>
      <li>
         <a href="/profil" data-drupal-link-system-path="profil"><i class="fas fa-id-card fa-lg"></i> Profils</a>
      </li>
   </ul>
</nav>

Here is my JS code. Something is wrong with it.

(function ($) {

  $(".collapse-change-icon").on('shown.bs.collapse', function () {
       $(this).find('[data-fa-i2svg]').removeClass("fa-plus-circle").addClass("fa-minus-circle");
  });

  $(".collapse-change-icon").on('hide.bs.collapse', function () {
       $(this).find('[data-fa-i2svg]').removeClass("fa-minus-circle").addClass("fa-plus-circle");
  });

})(window.jQuery);

Screenshot

enter image description here

Advertisement

Answer

You have not used the right bootstrap events for dropdown. Try this one.

            jQuery(document).ready(function ($) {
                $(".dropdown").on('shown.bs.dropdown', function () {    
                    $(this).find('[data-fa-i2svg]').removeClass("fa-plus-circle").addClass("fa-minus-circle");
                });

                $(".dropdown").on('hidden.bs.dropdown', function () {
                    $(this).find('[data-fa-i2svg]').removeClass("fa-minus-circle").addClass("fa-plus-circle");
                });
            });

REf: https://getbootstrap.com/docs/3.3/javascript/#dropdowns-events

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement