How to check if an element is hidden in jQuery?

There are many ways to check if an element is hidden using jQuery. I will list out mostly used methods in this post.

Let’s say the element has id=”test”. Then the code will be,

Method 1: 

$('#test').is(':hidden')

Working example for the above method is here:

<!DOCTYPE html>
<html>
	<head>
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
	<script>
		$(function(){
			if($('#test').is(':hidden')){
				alert('yes hidden');
			}
		});
	</script>
	</head>
	<body>
		<div class="container">
			<div class="row">
				<div id="test" style="display:none">Test DIV</div>
			</div>
		</div>
	</body>
</html>

Above snippet will see for the element is hidden or not, if hidden it gives an alert saying ‘yes hidden’.

 

Method 2: 

The second method is basically checking the CSS propery display is none or block.

$('#test').css('display') == 'none'

Working example for the method 2 is here:

<!DOCTYPE html>
<html>
	<head>
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
	<script>
		$(function(){
			if($('#test').css('display') == 'none'){
				alert('yes hidden');
			}
		});
	</script>
	</head>
	<body>
		<div class="container">
			<div class="row">
				<div id="test" style="display:none">Test DIV</div>
			</div>
		</div>
	</body>
</html>

Same like the above method this example also gives an alert ‘yes hidden’ if the display is none.

I hope this could have helped you.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top