How to use jquery to search for an element with display = none and return the id of the elements to a variable?

I use jquery to search for an element on a page with the display set to none and return its id in the variable. My attempt below:

$(".galleryitem[display='none']").this 

Can someone tell me where I'm wrong ...

+7
source share
5 answers

I don't think you need to add :hidden pseudo selector. The following will give you the selector id, whether it is hidden or not.

 var elementId = $(".galleryitem").attr("id"); 

but if you add it will be a bit faster-

 var elementId = $(".galleryitem:hidden").attr("id"); 
+8
source
  $(".galleryitem:hidden").attr("id"); 
+1
source

Since jQuery 1.3.2, an element is displayed if its offset-width or offsetHeight specified in the browser is greater than 0. What does this change mean? This means that if your CSS element is displayed as "none", or any of its parent / ancestor display elements are "none", or if the width of the elements is 0 and the height of the elements is 0, then the element will be reported as hidden.

Example:

This means that the .galleryitem element is recognized as hidden only if the parrent parameter has display: none style:

 var elementId = $(".parent .galleryitem:hidden").attr("id"); 

or

 var elementId = $(".galleryitem:hidden").attr("id"); 

You can choose the example that suits you best.

+1
source
 var elementId = $(".galleryitem:hidden").attr("id"); 
0
source

To find hidden elements, you can use :hidden psuedo selector.

 $(".galleryitem:hidden").each(function(){ //do something with each element. }); 

Or, if you have only one item, you can simply do the following:

 var id = $(".galleryitem:hidden")[0].id 

Jsfiddle example

0
source

All Articles