3

I'm using the latest jquery version.(1.9.1)

I have an empty span element :

<!DOCTYPE html>
 <html>

    <head>
        <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
        <meta charset=utf-8 />
        <title>JS Bin</title>
    </head>

    <body> <span class="c"></span>

    </body>
    <script type='text/javascript'>
        alert($(".c:visible").length);
    </script>

</html>
  • Chrome ( latest ver ) alerts 0
  • FF (latest ver) alerts 1
  • Ie (8) alert 0

Why doesn't it alert1 at chrome ? the element is not hidden.

I can work around this by using filter and spit out all the !display:none but wanted to know if i'm doing anything wrong

http://jsbin.com/urihov/2/edit

Royi Namir
  • 144,742
  • 138
  • 468
  • 792

1 Answers1

1

@inser is right a span is an inline element and only gains layout when it has content thus gaining the width and height of its content - in this case the width and height are 0 so not visible to the, if you add this css rule

.c {
  display: block;
}

you will see that the alert says 1 now since you have forced it to be a block element now thus inheriting the width of its parent and gaining layout.

hope it helps,

Cheers

Ady Ngom
  • 1,284
  • 2
  • 10
  • 12
  • Correct, but you need 0 width as well. So use `width: 0;` or `float: left;` – inser Mar 19 '13 at 07:28
  • actually if you add a width rule just to give it layout it is redundant since as block element via css it inherits the width of its parent which implies width: 100%; – Ady Ngom Mar 19 '13 at 07:32