Do you want to test whether an element is visible or hidden using jQuery? Here, you’ll learn three approaches to finding out if an element is visible or hidden with jQuery.
Table of Contents: Check if Element is Visible using jQuery
- Using jQuery’s .is() (Best Approach)
- Using jQuery’s .css() for display:none
- Using jQuery’s .css() for visibility:visible or visibility:hidden
- Video Tutorial & Demo
Using jQuery’s .is()
HTML
1 | <p class="wpp" style="display:none;">A test from inthiscode’s team</p> |
jQuery
1 2 3 4 5 6 7 | $(document).ready(function() { if($('.wpp’).is(":visible")) { alert("The paragraph with class 'wpp' is visible"); } else { alert("The paragraph with class 'wpp' is hidden"); } }); |
Using jQuery’s .css() for display:none
HTML
1 | <p class="wpp" style="display:none;">A test from inthiscode’s team</p> |
jQuery
1 2 3 4 5 6 7 | $(document).ready(function() { if ( $('.wpp’).css('display') == 'none' ){ alert("The paragraph with class 'wpp' is hidden"); } else { alert("The paragraph with class 'wpp' is visible"); } }); |
Using jQuery’s .css() for visibility:visible or visibility:hidden
HTML
1 | <p class="wpp" style="visibility:visible;">A test from inthiscode’s team</p> |
jQuery
1 2 3 4 5 6 7 | $(document).ready(function() { if ( $('.wpp').css('visibility') == 'hidden' ){ alert("The paragraph with class 'wpp' is hidden"); } else { alert("The paragraph with class 'wpp' is visible"); } }); |
Above methods are going to enable you to check if an element is hidden or visible using jQuery.
Video Tutorial & Demo
Last modified: June 6, 2017