question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Make jquery :hidden and :visible selectors work

See original GitHub issue

Hello,

I’m probably missing something here but I can’t make jQuery :hidden selector work.

Shouldn’t this print true ?

var jsdom = require("jsdom");

jsdom.env({
    html: '<div style="display: none;" />',
    scripts: [ "http://code.jquery.com/jquery-2.1.3.min.js" ],
    done: function (errors, window) {
        if (errors) throw errors;
        console.log(window.$('div').is(':hidden')); // prints false
    }
});

(jsdom version is 3.1.2)

Issue Analytics

  • State:open
  • Created 9 years ago
  • Reactions:1
  • Comments:13 (5 by maintainers)

github_iconTop GitHub Comments

5reactions
jcubiccommented, Jul 1, 2018

In jQuery 3 the visible pseudo selector look like this:

jQuery.expr.pseudos.visible = function( elem ) {
     return !!( elem.offsetWidth || elem.offsetHeight || elem.getClientRects().length );
};

so I’ve used this code:

window.Element.prototype.getClientRects = function() {
    var node = this;
    while(node) {
        if(node === document) {
            break;
        }
        // don't know why but style is sometimes undefined
        if (!node.style || node.style.display === 'none' || node.style.visibility === 'hidden') {
            return [];
        }
        node = node.parentNode;
    }
     var self = $(this);
    return [{width: self.width(), height: self.height()}];
};
1reaction
OmbraDiFenicecommented, Feb 27, 2022

expanding a little bit on the previous workaround I ended up with this, which seems to work also in the case of nested elements

window.Element.prototype.getClientRects = function() {
    let node = this;
    let visible = false;
    while(node) {
        if(node === document) {
            break;
        }
        // don't know why but style is sometimes undefined
        if (!node.style || node.style.display === 'none' || node.style.visibility === 'hidden') {
            return [];
        }
        visible = true;
        node = node.parentNode;
    }
    return visible ? [{width: 111, height: 111}] : [];
};

the aim is of course just to unblock :visible (and :hidden) jQuery selectors

Read more comments on GitHub >

github_iconTop Results From Across the Web

:visible Selector | jQuery API Documentation
This selector is the opposite of the :hidden selector. So, every element selected by :visible isn't selected by :hidden and vice versa.
Read more >
css - Using jQuery to select items that have style "visibility ...
jQuery selectors :visible and :hidden only respects display:none as really hidden? NOT visibility:hidden or visibility:visible.
Read more >
How to select all visible or hidden elements in a HTML page ...
In order to select all visible or hidden elements in a page using jQuery, we can use the following jQuery selectors:.
Read more >
jQuery :hidden Selector - W3Schools
The :hidden selector selects hidden elements. Hidden elements are elements that are: ... Note: This selector will not work on elements with visibility:hidden....
Read more >
How to Check an Element is Visible or not Using jQuery
You can use the jQuery :visible selector to check whether an element is visible in the layout or not. This selector will also...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found