jQuery Selector :hidden select all hidden elements in a page

Description

jQuery Selector :hidden select all hidden elements in a page

View in separate window

<!DOCTYPE html>
<html lang="en">
<head>
<title>jQuery Find Visible and Hidden Elements</title>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<style>
    .box{// www.  j  ava 2  s.c o  m
        padding: 50px;
        margin: 20px 0;
        display: inline-block;
        font: bold 22px sans-serif;
        background: #f4f4f4;
    }
    .hidden{
        display: none;
    }
</style>
<script>
$(document).ready(function() {
    // Get visible boxes
    $(".get-visible-btn").click(function(){
        var visibleBoxes = [];
        $.each($(".box"), function(){
            if($(this).is(":visible")) {
                visibleBoxes.push($(this).text());
            }

        });
        document.getElementById("demo").innerHTML = 
            "Visible boxes are - " + visibleBoxes.join(", ");
    });

    // Get hidden boxes
    $(".get-hidden-btn").click(function(){
        var hiddenBoxes = [];
        $.each($(".box"), function(){
            if($(this).is(":hidden")) {
                hiddenBoxes.push($(this).text());
            }
        });
        document.getElementById("demo").innerHTML += 
               "Hidden boxes are - " + hiddenBoxes.join(", ");
    });
});
</script>
</head>
<body>
    
    <p id="demo"></p>
    <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>
    <br>
    <button type="button" class="get-visible-btn">Get Visible Boxes</button>
    <button type="button" class="get-hidden-btn">Get Hidden Boxes</button>
</body>
</html>



PreviousNext

Related