jQuery's visibility check function is not functioning properly

I am developing a web application for managing orders and finance in a restaurant. To enhance the functionality of my application, I require more screens to operate with. To facilitate this, I have implemented a small function to toggle between visibility:

function switchTab(toActivate, toDeactivate) {
    var $toActivate = $(toActivate);
    var $toDeactivate = $(toDeactivate);

    if ($toDeactivate.is(":visible")) {
        $toDeactivate.css("visibility", "hidden");
        $toActivate.css("visibility", "visible");
    }
}

In addition, I have also designed a function that allows for deleting existing orders. This function is utilized to delete an order while on the screen for creating one.

function delete_order() {
    if (confirm("Are you sure you want to delete this order?")) {
        if ($("#new-order").is(":visible")) {
            switchTab(orderListScreen, newOrderScreen);
            switchTab(orderInformationScreen, availableProductsScreen);

            alert("Only this is getting called");
        } else if ($("#list").is(":visible")) {
            $(".selectedRow").remove();

            alert("Why doesnt this get called?");
        }
    }
}

Issue

Whenever I create and then delete an order using the delete button triggering the delete_order() function, it works well as it refers to the correct section of the code (

if (newOrderScreen.is(":visible"))
). However, when attempting to delete an existing order, it fails to execute correctly, repeating the upper portion of the code instead.

The snippet of code to bring up a new order screen is as follows:

function new_order() {
    switchTab(newOrderScreen, orderListScreen);
    switchTab(availableProductsScreen, orderInformationScreen);
}
  • Note: The .selectedRow class is assigned when selecting a row.

  • Note: The #list screen is displayed by default, while the #new-order screen is only shown upon clicking a respective button.

The CSS styling for the two switching screens is outlined below:

#new-order {
    position: absolute;
    left: 0;
    top: 0;
    width: 40%;
    z-index: 1;
    visibility: hidden;
}

#list {
    display: block;
}

Query

Why does the code not select the right part of the if statement for existing orders? And why does it function correctly when displaying the new order screen?

Answer №1

According to the documentation found in the JQuery API:

If elements have visibility: hidden or opacity: 0, they are still considered visible because they take up space in the layout.

You can find more information on this topic here.

This is why your if statement may not be functioning as expected.

In your initial if statement, instead of checking for visibility, verify whether new-Order is set to visibility:hidden or not.

I also spoke with some JavaScript experts who suggested using jQuery .hide() to change it to :visible before executing your if statement.

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

Using multiple background images in CSS on mobile devices

I decided to experiment with creating a grid pattern using css background-images in this way: .grid { width:720px; height:720px; background-color: #333333; background-image: linear-gradient(rgba(255,255,255,0.1), 1px, transparent 1px), ...

Creating a two-column post loop in Wordpress is a great way to display content in a

I've been attempting to separate posts for a long time without success. No matter what variations I try, the result is always either a single post or duplicating all of them. In other words, multiple posts are not displaying as intended. If anyone ha ...

"Interacting with the ng-keypress event causes the page to smoothly scroll

As a newcomer to angularjs, I am trying to figure out why my component (which simulates a "checkbox" using fontawesome icons) causes the page to scroll down when the user presses the space key. Here is a snippet of my component : ugoFmk.component(' ...

Tips for fetching a particular item from a getItem dynamoDB in JavaScript

I am trying to retrieve an item from my AWS database. The item 'test2' is printing correctly in my console. However, I want to extract an attribute or variable from this item and store it as a variable named test. How can I accomplish this? For i ...

Tips for concealing a Donut Chart when the value is zero and displaying the chart in grey when there is an empty value using

I've recently switched from using ChartJs to EchartsJS in my project, but I'm facing a specific challenge. When all items in the chart have a value of 0, EchartJs still displays all colors equally. However, I want the chart to be hidden in such ...

Tips for concealing validation errors in React Js when modifying the input field

I have recently started working with ReactJs, and I've implemented form validation using react-hook-form. After submitting the form, the errors are displayed correctly. However, the issue arises when I try to update the input fields as the error messa ...

How can we bring in prop array values in React?

I've been working on developing a small music player program in React. Is there a way to import all these values together with a single import statement? I noticed that manually inputting the values into the props array doesn't work because the ...

Tips for adding a CSS class to an HTML element on-the-fly

As a newcomer to JavaScript and AngularJS, my question may be considered naive by some. I am currently working on a tutorial project involving AngularJS. Here is the HTML code snippet I am dealing with: <link href="http://netdna.bootstrapcdn.com/boo ...

Navigating the reactive array in Vue 3 with finesse

Currently, I'm facing some challenges with my Vue 3 app in terms of getting things to work as expected. The main issue lies in properly accessing a reactive array. Let me provide you with the code for better comprehension: Within my global store sto ...

Using JavaScript to control a dropdown list based on the selection of another dropdown

Hello, I am new to programming and a JavaScript beginner seeking help from you all. I have two dropdown lists and I am attempting to manipulate one based on the selection of the other using JavaScript. Both dropdown lists contain time in hours denoting st ...

I am searching for a way to apply a conditional class to the chosen element in react, as the toggle method does not

I'm working on customizing a dropdown menu and I want to add a functionality where if a parent li menu has an arrow class before the ul element, then clicking on that parent li menu will add a class called showMenu only to that specific sub-menu. Her ...

Unable to retrieve scope variable within AJAX call

I am currently in the process of running a loop function that contains an AJAX request within it. However, I am facing difficulties with the success function on the AJAX not being able to access a counter variable outside of the loop function. var upda ...

What could be the reason for the full name not being updated in the model when the last name is changed

Can you explain why the full name in the model does not update when I change the last name? You can view the code on Plunker: http://plnkr.co/edit/cqmwJc5dpoDWvai4xeNX?p=preview var loginCntrl=function($scope){ $scope.testClick =function(){ $scope. ...

What are the best methods for adjusting the size of a game's

I create games using HTML5/CSS/JS, but I am struggling to figure out how to make them scale to different screen resolutions. It seems like a simple task, but I can't seem to grasp it. SOLVED var RATIO = 480 / 800; // Initial ratio. function resize() ...

What is the process for extracting HTML content using the JavaScript executor?

import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; public class WebDriverExample { public static void main(String[] args) { System.setProperty("webdriver.c ...

Here is a guide on how to develop a PHP function that interacts with a textarea to display text in the specified color by using syntax like [color:red]

Is it possible to code a PHP function that can work alongside a textarea to display text in the specified color by using a syntax like [color:red]? This function operates in a similar manner to Facebook's @[profile_id:0] feature. ...

The client sent a risky Request.Path value that raised red flags

For a university project, I am attempting to access an English translation of the Quran from quran.com using the provided link: Although this link works correctly when entered directly into a browser, I encounter an error when trying to use it in my Angul ...

Adjust padding for smaller devices in React using Material UI

In my grid layout, I have columns set to 3,6,3 and a spacing of 3 between them. On larger screens, the spacing between grids looks fine. However, as the screen size decreases, the spacing remains the same which is not visually appealing. What I am aiming ...

405 error: NGINX blocking POST method in Django DRF Vue.js application

I have encountered a strange issue while building my ecommerce site. Everything seems to be working fine, except for the forms. When attempting to post content, I keep receiving a 405 method get not allowed error. It's confusing as I am trying to use ...

"Encountering issues with toggle effect in nested divs when utilizing an Angular Directive -

I have included a link to the Plunker. Unfortunately, I am facing issues with accessing the elements .gc and .mc. On the other hand, the class .bc is functioning properly. My goal is to implement a toggle effect on them based on hierarchy: BroadCategory ...