The functions .is("visible") and .is(":visible") seem to be behaving in distinct ways

While working with the jquery visible selector, I noticed a strange inconsistency when checking if a child element is visible or not. When using .is("visible") and .is(":visible") with the css visibility:hidden property, they produced different results. With .is('visible'), it alerted false, but with .is(":visible"), it alerted true. However, when changing the css property to display:none, the result was consistent. Here is the code snippet:

HTML

<div id="parentEle">
    I have hidden span
    <span class="hiddenContent">
        I am hiddenContent
    </span>
</div>
<button type="button" onclick="_checkChild()">Check Child</button>

JS

function _checkChild(){
    var x= false;
    x =$("#parentEle").children(".hiddenContent").is(":visible");
    alert(x);
}

CSS

.hiddenContent{
   visibility:hidden
}

jsfiddle

If anyone can shed light on this discrepancy, I would greatly appreciate it. Thank you!

Answer №1

.is(":visible"); is a unique selector in jQuery that determines if an element is currently visible, with results subject to change over time. On the other hand, using .is("visible"); as a selector would only check if the element is <visible />, providing no information about its actual visibility status.

Answer №2

Let me clarify the difference between .is(":visible") and .is("visible"). The former checks if an element is visible on the page, while the latter checks if the element is a visible tag.

For example, using .is("span") will return true if the element is a span tag in your code.

Answer №3

.is("visible"); will search for a <visible> tag. The correct way to check if an element is visible is by using .is(":visible");. Even elements with visibility: hidden or opacity: 0 are considered visible because they still occupy space in the layout (refer to https://api.jquery.com/visible-selector/).

The only scenario where .is(":visible")==false; occurs is when an element does not take up any space in the layout, such as when its height and width are set to 0 or when display: none is used (which essentially makes the element's dimensions 0).

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

I'm trying to integrate a chatbot into my website using Node-RED. Can someone guide me on how to utilize the Bluemix Tone Analyzer to analyze the tone of the user

Creating a chat bot to ask psychological questions and analyze how a person is feeling has been my recent project. I managed to set up a chat bot in Bluemix, following a tutorial on integrating it into Node Red: https://github.com/watson-developer-cloud/no ...

Exploring the wonders of Angularjs and the dynamic capabilities of streaming data

Using AngularJS's $http to request streaming data like this: $http({ method: 'POST', url: url, data: JSON.stringify(data), headers: config }).success(function(responseData) { console.log(responseData); }).error(funct ...

How can you use jQuery to display an image when hovering over text?

Looking for a tutorial or script that displays an image when hovering over HTML text with the mouse? ...

Tips for understanding JSON responses using jQuery

I have received a JSON response string from my handler, and I am looking to extract the value of the Name field in an alert for testing purposes. How can I achieve this? The string is shown below: { "files": [ { "Thumbnail_url": n ...

iOS Chrome: Enabling Cookies with "Always Allow"

While the Safari browser on OSX has a setting under Privacy & Security -> Block Cookies -> Always Allow, enabling the storage of entries in the browser's local storage even when accessing pages from third party sites like those running in an ifr ...

Sorting arrays with nested objects in JavaScript

I am facing a challenge with sorting an array of objects in two specific ways: First, I need to sort the objects by alphabetical order using the country name. Then, inside each country, I have to arrange the scores in ascending order. Below you can find ...

Struggling to center the hidden divs both vertically and horizontally on the page when they are made visible

I'm having trouble aligning my hidden divs in the center of the page. When I set their position to relative, the scroll increases as I move from one page to another (meaning the div is not vertically centered on the page). Should I place all three d ...

Drag items with jQuery without needing to click on them

I am working on a draggable div and currently using the jQuery UI .draggable function. However, this doesn't quite meet my requirements. $('.magic_background').draggable({ cursor: 'move', containment: &apo ...

Retrieving chosen items from NextUI-Table

I am completely new to JavaScript and NextUI. My usual work involves C# and DotNET. I have a requirement to create a table with selectable items, and when a button is clicked, all the selected items should be passed to a function on the server that accepts ...

After a certain time has passed, an event will occur once a div element has been assigned

Is there a way to show div2 only after div1 has been given the '.selected' class for a set duration, and then hide it again when div1 loses the '.selected' class? What would be the most efficient approach to achieve this? ...

`The Streaming module within React Native`

I am facing an issue in my React Native project where I want to use a node library that depends on stream (require('stream')). The problem arises with the error stream could not be found within the project because stream is a nodejs package not ...

Utilize jQueryUI to seamlessly integrate email functionality into CakePHP

I am looking to automate email notifications to both administrators and users upon completion of a jQuery UI form submission. The form, which is located in the view.ctp file, looks like this: <div id="dialog-form" title="Create new user"> < ...

Tips for transferring data from a service to a method within a component

I have a service that successfully shares data between 2 components. However, I now need to trigger a method in component A when an event occurs on the service (and pass a value to that component). Can someone guide me on how to achieve this? I have seen ...

Different ways to update background hues in Reactstrap

I am currently working on styling an application with reactstrap, and I'm facing a challenge in changing the background color of the navigation bar from white to black. I tried setting the color attribute to "dark" in the nav tabs tag, but it didn&apo ...

flawless approach to showcasing visual content

I am a beginner in css. My goal is to showcase my images in an optimal way This is the HTML code I have: <div id="photos"> <h3>Title of the Photo</h3> <P class="like"><a href="#">Give it a Like</a& ...

Step-by-step guide for implementing tooltips using jQuery

I've been attempting to implement a tooltip using jQuery UI. However, when I use var_dump I am not getting any output. Here is the code snippet: <a href="#"><span id='11111_22222'>text_here</span></a> And this is ...

Having trouble with collapsing elements on JavaScript?

I am encountering an issue with the code provided below. It seems to be working fine in this JSFiddle link: http://jsfiddle.net/e6kaV/33/. However, I am receiving the following error message: { "message": "Uncaught ReferenceError: $ is not defined", ...

Vertical and floating alignment

Can you help me with this HTML/CSS challenge? User: Support: Emily Johnson Alex Roberts I am looking to have two input boxes aligned vertically on both the left and right ...

Is it possible to remove certain 'css-' class names that MUI automatically applies to its components? (Using Next JS and MUI)

After successfully migrating my create-react-app project to Next JS using the latest version (12.1.0) and following the migration guide at https://nextjs.org/docs/migrating/from-create-react-app, I encountered an unexpected issue. Despite still using MUI a ...

Adapting my JavaScript code to handle one-dimensional CSV data instead of the usual two-dimensional format

How can I effectively parse this CSV file using JavaScript? 1363085391,42.890000000000,5.432200000000 1363088879,47.570000000000,4.981800000000 1363120475,56.560000000000,1.768000000000 1363132522,53.000000000000,1.000000000000 1363214378,48.630000000000, ...