How to check an element is visible or not using jQuery

Topic: JavaScript / jQuery« Prev|Next »

Answer: Use the jQuery :visible Selector

You can use the jQuery :visible selector to check whether an element is visible in the layout or not. This selector will also select the elements with visibility: hidden; or opacity: 0;, because they preserve space in the layout even they are not visible to the eye.

  • <!DOCTYPE html>
  • <html lang="en">
  • <head>
  • <meta charset="utf-8">
  • <title>jQuery Test If an Element is Visible</title>
  • <script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
  • <script type="text/javascript">
  •     $(document).ready(function(){
  •         $("button").click(function(){
  •             // show hide paragraph on button click
  •             $("p").toggle("slow", function(){
  •                 // check paragraph once toggle effect is completed
  •                 if($("p").is(":visible")){
  •                     alert("The paragraph  is visible.");
  •                 } else{
  •                     alert("The paragraph  is hidden.");
  •                 }
  •             });
  •         });
  •     });
  • </script>
  • </head>
  • <body>
  •     <button type="button">Toggle Paragraph Display</button>
  •     <p style="display: none;">Lorem ipsum dolor sit amet adipi elit...</p>
  • </body> 
  • </html>

Related FAQ

Here are some more FAQ related to this topic:

 
Close

Your Feedback:

 

We would love to hear from you! Please say something.