jQuery Selector :hidden check if an element is hidden

Introduction

The jQuery :hidden selector tells whether an element is hidden or not.

View in separate window

<!DOCTYPE html>
<html lang="en">
<head>
<title>jQuery Test If an Element is Hidden in a Page</title>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script>
    $(document).ready(function(){
        $("button").click(function(){
            $("p").toggle("slow", function(){
                if($("p").is(":hidden")){
                    document.getElementById("demo").innerHTML 
                          = "The paragraph is hidden.";
                } else{/*from  w w w. jav a  2s.  c o  m*/
                    document.getElementById("demo").innerHTML 
                          = "The paragraph is visible.";
                }
            });
        });
    });
</script>
</head>
<body>
    <span id="demo"></span>
    <button type="button">Toggle Paragraph Display</button>
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. 
    Nam eu sem tempor, varius quam at, luctus dui. Mauris magna 
    metus, dapibus nec turpis vel, semper malesuada ante. 
    Vestibulum id metus ac nisl bibendum scelerisque non non purus. 
    Suspendisse varius nibh non aliquet sagittis. In tincidunt orci 
    sit amet elementum vestibulum. Vivamus fermentum in arcu in aliquam. 
    Quisque aliquam porta odio in fringilla. Vivamus nisl leo, blandit 
    at bibendum eu, tristique eget risus. Integer aliquet quam ut elit 
    suscipit, id interdum neque porttitor. Integer faucibus ligula.</p>
</body>
</html>



PreviousNext

Related