Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use jquery to find the element with display=none and return the elements id to a variable?

Tags:

jquery

I am using jquery to to find the element on the page with display set to none and return it's id in a variable. My attempt is below:

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

Can someone tell me where I am going wrong...

like image 893
David Avatar asked Jul 28 '11 12:07

David


3 Answers

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

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

but if you add it will be bit faster-

var elementId = $(".galleryitem:hidden").attr("id");
like image 174
Vivek Avatar answered Sep 30 '22 17:09

Vivek


  $(".galleryitem:hidden").attr("id");
like image 26
Vivek Goel Avatar answered Sep 30 '22 17:09

Vivek Goel


Since jQuery 1.3.2, an element is visible if its browser-reported offsetWidth or offsetHeight is greater than 0. What does this change mean? It means that if your element’s CSS display is “none”, or any of its parent/ancestor element’s display is “none”, or if the element’s width is 0 and the element’s height is 0 then an element will be reported as hidden.

Example:

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

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

or

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

You can choose the example that works best for you.

like image 32
Liviu Dragulin Avatar answered Sep 30 '22 17:09

Liviu Dragulin