drup8
drup8

Reputation: 5

How to delete a class with JS if an element is not present?

On my site I have a menu to the right of the navigation bar. It displays the search when it is available on the current page.

I want to display an icon when the search is available.

All search blocks have the class .views-exposed-form and appear in #navbar-collapse-second

I added to my icon, the class .icon-navbar-alert-disable which is used to hide the icon.

How to remove the .icon-navbar-alert-disable class when the .views-exposed-form class is present in #navbar-collapse-second ?

Here is the css code to hide the icon :

.region-navigation-menu-second .navbar-toggle-second .icon-navbar-alert-disable {
    display: none;
}

Here is the code of the button of my manu with the icon :

<div{{ attributes }}>

<a class="navbar-toggle-second collapsed" data-toggle="collapse" data-target="#navbar-collapse-second" aria-expanded="false">

  <div class="icon-navbar">
    <span class="fa-layers fa-3x">
      <i class="far fa-circle"></i>
      <span class="navbar-icon-open">
        <i class="fas fa-filter" data-fa-transform="shrink-9"></i>
      </span>
      <span class="navbar-icon-close">
        <i class="fas fa-times" data-fa-transform="shrink-8"></i>
      </span>
    </span>
  </div>

  <div class="icon-navbar-alert icon-navbar-alert-disable">
    <span class="fa-stack fa-lg">
      <i class="fas fa-circle fa-stack-2x"></i>
      <i class="fas fa-filter fa-stack-1x fa-inverse"></i>
    </span>
  </div>

</a>

</div>

Here is the page concerned, this is the menu on the right :

https://www.s1biose.com/boutique/ma-boutique-pro

I started a JS code but it is not complete :

  $('#navbar-collapse-second') ???.views-exposed-form??? {
       $('#block-togglenavigationsecond').removeClass('icon-navbar-alert-disable');
  });

UPDATE

(function ($) {

  if ($('#navbar-collapse-second').hasClass('views-exposed-form')) {
       $('#block-togglenavigationsecond').removeClass('icon-navbar-alert-disable');
  } else { 
       $('#block-togglenavigationsecond').addClass('icon-navbar-alert-disable');
  };

})(window.jQuery);

enter image description here

Upvotes: 0

Views: 76

Answers (1)

Clams Are Great
Clams Are Great

Reputation: 280

Are you looking for the hasClass selector in jQuery?

If so wrap it in a if statement and inside you can show the result you'd like from it;

if ($('#navbar-collapse-second').hasClass('views-exposed-form') === false) {
    $('#block-togglenavigationsecond').removeClass('icon-navbar-alert-disable');
} else { 
    $('#block-togglenavigationsecond').addClass('icon-navbar-alert-disable');
};

Upvotes: 1

Related Questions