0

In jQuery, I can use $(':hidden') to find hidden elements, but I don't want to include the 0 size elements (width=0 and height=0).

How to achieve this?

I can't just judge if it is 0 size, because if one of its parents is hidden, I also want to select it.

For example:

<img id="e1" width="0" height="0" />
<div style="display:none;">
    <img id="e2" width="0" height="0" />
</div>

<script>
    // I want to select "e2",but not "e1"
    var myelems = $('img:hidden');
</script>

I want to select "e2",but not "e1".

Lixiao
  • 1
  • 3

5 Answers5

0

use filter in jquery

$(':hidden').filter(function(){
      return $(this).width()!=0 && $(this).height()!=0;
});
Balachandran
  • 9,567
  • 1
  • 16
  • 26
0

Something like this?

if (element.is(':hidden') && element.width() != 0 && element.height != 0) {
   //do some stuff
}
Aramil Rey
  • 3,387
  • 1
  • 19
  • 30
0

You can try using the not method or not selector of jQuery to solve your issue. For example,

$(document).ready(function() {
  console.log($(":hidden").not($("input[width=0],input[height=0]")));

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h1>Check the output in console</h1>
<input type="hidden" name="a1" id="a1" value="a1">
<input type="hidden" name="a2" id="a2" value="a2">
<input type="hidden" name="b1" id="b1" width=0 height=0 value="a3">

Try it!!

Arcanyx
  • 852
  • 10
  • 25
  • Lol! That is just an example! Hidden input elements do not have **width** and **height** either!!! – Arcanyx May 29 '15 at 07:06
0

If you set the style attribute to display: none; (or use jQuery's .hide()) you can use this

(Demo)

$('*[style*="display: none;"]');
0
jQuery('elem:hidden').each(function(ind){
if(jQuery(this).height() > 0 && jQuery(this).width() > 0){
console.log(jQuery(this))
}
});
Dusty
  • 354
  • 4
  • 15