:hidden selectorThe jQuery :hidden selector is used to select all the elements that are hidden.
display:none;type="hidden" form element;0 ; ( Older jquery version.For example:1.9.0 )| style | visible/hidden |
|---|---|
display:none |
hidden |
display:block |
visible |
width:0;height:0 |
hidden(jQuery ver1.9) , visible(jQuery ver3.4) |
width>0;height:0 |
visible |
width:0;height>0 |
visible |
visibility:hidden |
visible |
opacity:0 |
visible |
type="hidden" |
hidden |
In some browsers :hidden includes head, title, script, etc.
You can use .not() to exclude them.
var hiddenElements = $( "body" ).find( ":hidden" ).not( "script" );
The element with visibility: hidden or opacity: 0; is considered visible,because it takes space in the flow of the document.
:hidden selector Syntax$(':hidden')
:hidden selector ExamplesFollowing is a simple example which would count hidden inputs and divs.
Try now
Use $('selector').is(":hidden") or $(element).is(":visible") to check if an element is hidden. Please note the situations that an element is considered hidden as mentioned above.
An element with display:none has no dimensions. If you want it hidden but measurable, use visibility: hidden or opacity: 0; .
If you want to get the width of an element with display:none, you can temporarily make it visible but out of viewport. It does not affect the user experience due to it's hidden.
// store the original attributes temporarily.
var origStyle = $("#myDiv").attr("style"),
newStyle = ';position: absolute !important; top: -1000px !important; display: block !important;';
$("#myDiv").attr( 'style', origStyle+newStyle );
// get width and height
var elemWidth = $("#myDiv").width();
var elemHeight = $("#myDiv").height();
// restore the attributes
$("#myDiv").attr("style", origStyle ? origStyle : "");
console.log(elemWidth);
console.log(elemHeight);
