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


Use the jQuery :visible and :hidden Selector

You can simply use the jQuery :visible or :hidden selector to select all the visible or hidden elements in an HTML page. The jQuery :visible selector considered an element visible if they consume space in the document. That means, elements with visibility: hidden; or opacity: 0; are considered visible, since they still preserve space in the layout.

Let's try out the following example to understand how this basically works:

<!DOCTYPE html>
<html lang="en">
<meta charset="utf-8">
<title>jQuery Get All Visible and Hidden Elements</title>
<script src=""></script>
        padding: 50px;
        margin: 20px 0;
        display: inline-block;
        font: bold 22px sans-serif;
        background: #f4f4f4;
        display: none;
$(document).ready(function() {
    // Get visible boxes
        var visibleBoxes = [];
        $.each($(".box"), function(){ 
            if($(this).is(":visible")) {
        alert("Visible boxes are - " + visibleBoxes.join(", "));
    // Get hidden boxes
        var hiddenBoxes = [];
        $.each($(".box"), function(){ 
            if($(this).is(":hidden")) {
        alert("Hidden boxes are - " + hiddenBoxes.join(", "));
    <div class="box">Box A</div>
    <div class="box hidden">Box B</div>
    <div class="box">Box C</div>
    <div class="box hidden">Box D</div>
    <div class="box">Box E</div>
    <button type="button" class="get-visible-btn">Get Visible Boxes</button>
    <button type="button" class="get-hidden-btn">Get Hidden Boxes</button>