Verify the visibility of the toggle, and eliminate the class if it is hidden

I have implemented two toggles in the code snippet below. I am trying to find a solution to determine if either search-open or nav-open are hidden, and if they are, then remove the no-scroll class from the body element.

$(document).ready(function() {
  $('.nav-button').click(function() {
    $('body').removeClass('search-open no-scroll');
    $('body').toggleClass('nav-open no-scroll');
  });

  $('.search-button').click(function() {
    $('body').removeClass('nav-open no-scroll');
    $('body').toggleClass('search-open no-scroll');
  });

});
.nav-open {
  opacity: 1;
  visibility: visible;
  background: rgba(76, 182, 204, 1);
  -webkit-transition: all 0.3s;
  transition: all 0.3s;
}

.search-open {
  opacity: 1;
  visibility: visible;
  background: rgba(201, 40, 59, 1);
  -webkit-transition: all 0.3s;
  transition: all 0.3s;
}

.no-scroll {
  overflow: hidden;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

  <div>
    <a class="search-button">Search</a>
  </div>

  <div>
    <a class="nav-button">Navigation</a>
  </div>
  <br /><br /> Content
  <br /> Content (repeated multiple times for demonstration purposes)
  <br />

Answer №1

It seems that toggling the no-scroll class twice is causing the issue as it has no effect. Are you trying to achieve a different result?

$(document).ready(function() {
  $(".nav-button").click(function() {
    $("body").removeClass("search-open no-scroll");
    $("body").toggleClass("nav-open");
    if ($("body").hasClass("nav-open") || $("body").hasClass("search-open")) {
      $("body").addClass("no-scroll");
    }
  });

  $(".search-button").click(function() {
    $("body").removeClass("nav-open  no-scroll");
    $("body").toggleClass("search-open");
    if ($("body").hasClass("nav-open") || $("body").hasClass("search-open")) {
      $("body").addClass("no-scroll");
    }
  });
});
.nav-open {
  opacity: 1;
  visibility: visible;
  background: rgba(76, 182, 204, 1);
  -webkit-transition: all 0.3s;
  transition: all 0.3s;
}

.search-open {
  opacity: 1;
  visibility: visible;
  background: rgba(201, 40, 59, 1);
  -webkit-transition: all 0.3s;
  transition: all 0.3s;
}

.no-scroll {
  overflow: hidden;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
  <a class="search-button">Search</a>
</div>

<div>
  <a class="nav-button">Navigation</a>
</div>
<br /><br /> Unique Content Here
<br />

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Replacing a div with another div can be achieved in a single swap

My goal is to swap one div with another div upon clicking using the provided code. It functions properly for a single instance. However, the issue arises when the class appears multiple times, causing all instances to change due to sharing the same class n ...

Why is the lower right white space left unfilled by the color red?

I'm having issues with using named lines in grid for layout. The red section is not positioning correctly next to the footer, and I can't figure out where I went wrong. * { box-sizing: border-box; margin: 0; padding: 0; } .container { ...

Minimize white spaces when validating input fields in a form using Javascript

I'm currently facing a challenge with JavaScript, specifically regarding achieving five tasks without using jQuery. Despite trying various RegExp codes, none have successfully worked for me so far. Let's begin with the first task (number 1): El ...

Modify the button's text with jQuery after it has been clicked

I've been struggling to update the text of a button after it's clicked, and I'm encountering an issue where it works in one part of my code but not in another. Could someone please guide me on what might be missing in my code? The first bl ...

How can I retrieve a value from a jQuery function within an external Angular function?

implement{ let selection; $(".l_item").click(function(){ selection = this.value; }); console.log(this.selection); } This unique jQuery function retrieves the selected value from the l_item array of lists. After obtaining the value, there ...

What is the reason behind JavaScript's `fn.length` returning the count of named parameters that `fn` function has?

Why does calling fn.length in JavaScript return the number of named arguments fn has? > function fn () { } > x.length 0 > function fn (a) { } > x.length 1 > function fn (a,b,c) { } > x.length 3 This behavior is quite peculiar. I wonde ...

JavaScript seems to have difficulty correctly parsing objects from JSON

Having trouble populating a Repeat Box with objects from a JSON document. Despite my efforts, it keeps duplicating the first item instead of adding each one individually. Any ideas on what might be causing this issue? Below is the code I am currently usin ...

Obtaining the node generated from a document fragment

Suppose I add a document fragment to the DOM in the following way: const ul = document.querySelector("ul"); const fruits = ["Apple", "Orange", "Banana", "Melon"]; const fragment = new DocumentFragment(); ...

Having troubles with PHP retrieving images from my server directory over here

My current code is designed to retrieve an image from my server and display it on my HTML page. However, the images are stored in an encrypted format using MD5 and do not have corresponding names in the table. To fetch the image, I am using the primary key ...

Masking of the Navigation Button

I designed a responsive navigation bar, but in mobile view, the menu icon is being hidden by the menu headings when displayed. Check out the scenario below with a provided CodePen link. I attempted to adjust padding and float properties without success. An ...

Necessary form group in html [Angular]

Is there a way to make certain form fieldsets required while allowing the user to choose which inputs to fill? My idea: Users can select what they want to fill out without having to complete all of the fieldset inputs. I have these two options: 1: < ...

PhoneGap fails to fire the ondeviceready event within 5 seconds

Currently, I am in the process of debugging my PhoneGap app using Weinre and facing some persistent errors... The 'deviceready' event has not fired even after 5 seconds. Channels are not firing: onPluginsReady, onCordovaReady, onCordovaConnecti ...

The URL in an AJAX request includes a repeating fragment due to a variable being passed within it

const templatePath = envVars.rootTemplateFolder + $(this).attr('link'); console.log("templatePath : ", templatePath); $.ajax({ method: "GET", url: templatePath, context: this, success : function(result){ ...

Issue with host header detected in MERN stack configuration

"proxy": "https://mango-artist-rmdnr.pwskills.app:5000", While attempting to establish a connection between my frontend and backend, I encountered an issue with an invalid host header. The backend is operating on port 5000, and the fr ...

I prefer not to have the entire URL visible to me

function updateDate(day, month, year) { month += ""; if (month.length <= 1) month = "0" + month; document.location.href = "<?php $_SERVER['PHP_SELF'];?>?page=events&day=" + day + "&month=" + m ...

Ways to deactivate just the Clicked button in a mapped over component

Utilizing the request variable, I am displaying an array of objects on the page. Each object in the array contains properties such as question, correct_answer, and incorrect_answer. Moreover, I have implemented a state called disable which toggles between ...

How to use jQuery to dynamically assign a class to an li element

I'm attempting to use jQuery to add the 'block' class to specific li elements, but for some reason the class isn't being applied. The goal of the program is to display time slots and disable certain ones if they are blocked. Here's ...

Implementing Firestore Read Limitations in a React Application

I have encountered an issue with Firebase billing based on the number of document reads. There is a daily limit of 50k reads per day in Firestore, but when I try to fetch documents in my React app, it triggers a quota exceeded error. FirebaseError: Request ...

What is the trick to getting an accordion to expand when hovering, rather than when clicking?

Can the accordion be set to expand when hovering instead of clicking? Also, how can a different action be triggered on click? LATEST I was specifically referring to using the jQuery UI accordion widget for this functionality. ...

Preventing the "save" button from being enabled until a change has been made to at least one input field

I have a page with approximately 20 input fields, along with save and register buttons. Is there a way to activate the "save" button only when a change has been made in at least one of the fields? ...