How to select all visible or hidden elements in a HTML page using jQuery ?

In order to select all visible or hidden elements in a page using jQuery, we can use the following jQuery selectors:

:visible Selector The visible Selector is used to select all the elements that are currently visible in the document.

  • Syntax:
    $(":visible") 

:hidden Selector The hidden selector selects hidden elements to work upon.

  • Syntax:
    $(":hidden")

Below example illustrates the above approach:
Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>
        How to select all visible
        or hidden elements in a
        HTML page using jQuery ?
    </title>
  
    <script src=
    </script>
      
    <style>
        h1 {
            color: green;
        }
          
        h4 {
            color: green;
        }
          
        body {
            text-align: center;
        }
          
        .geeks {
            display: inline-block;
            font: bold 16px sans-serif;
            color: green;
        }
          
        .hidden {
            display: none;
        }
    </style>
</head>
  
<body>
    <h1>GeeksforGeeks</h1>
      
    <h3
        How to select all visible or
        hidden elements<br>in a HTML
        page using jQuery
    </h3>
      
    <div class="geeks">GeeksforGeeks</div>
      
    <div class="geeks hidden">
        A Computer Sciecne Portal
    </div>
      
    <div class="geeks">For Geeks</div>
      
    <div class="geeks hidden">
        Learn Contribute Explore
    </div>
    <br>
      
    <button type="button" class="visibleg">
        Visible
    </button>
      
    <button type="button" class="hiddeng">
        Hidden
    </button>
    <br>
      
    <h4></h4>
      
    <script>
        $(document).ready(function() {
            $(".visibleg").click(function() {
                var visibleBoxes = [];
                $.each($(".geeks"), function() {
                    if ($(this).is(":visible")) {
                        visibleBoxes.push($(this).text());
                    }
                });
                $("h4").text("Visible items are - "
                            + visibleBoxes.join(", "));
            });
  
            // Get hidden items
            $(".hiddeng").click(function() {
                var hiddenBoxes = [];
                $.each($(".geeks"), function() {
                    if ($(this).is(":hidden")) {
                        hiddenBoxes.push($(this).text());
                    }
                });
                $("h4").text("Hidden items are - " 
                            + hiddenBoxes.join(", "));
            });
        });
    </script>
</body>
  
</html>

chevron_right


Output:

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.