How to check if an element is hidden in jQuery
In this tutorial, we are going to see how to check if an element is hidden in jQuery. :hidden selector in jQuery can be used to test whether or not an element is hidden on a page. You can simply use this selector to test elements which thay have width and height set to 0, as well as form elements with type = "hidden"
attribute.
HTML Code:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>How to check if an element is hidden</title> <script src="https://code.jquery.com/jquery-1.12.4.min.js"></script> <script type="text/javascript"> // Put the jQuery code Here </script> </head> <body> <button type="button">Show / hide paragraph</button> <p>This is a paragraph</p> </body> </html>
jQuery Code:
$(document).ready(function() { $("button").click(function() { $("p").toggle("slow", function() { if ($("p").is(":hidden")) { alert("The paragraph is hidden."); } else { alert("The paragraph is visible."); } }); }); });
Result |
---|
This is a paragraph |