jQuery Selector How to - Select all elements that are visible








Question

We would like to know how to select all elements that are visible.

Answer


<!--   w  w  w.java 2 s .c o m-->
<html>
  <head>
    <script type="text/javascript" src='http://code.jquery.com/jquery-1.5.2.js'></script>
    <script type="text/javascript">
        $(document).ready(function(){
            $("div:visible").click(function () {
                $(this).css("background", "yellow");
            });
        });
    </script>
  </head>
  <body>
      <span></span>
      <div style="display:none;">Hider!</div>
      <div>click me</div>
  </body>
</html>

The code above is rendered as follows: