Online Snippets Library

Check div is empty or not using jQuery

Some methods for checking div is empty or not are given below.

if ($('#element').is(':empty')){ //do something} if( $('#element:empty').length ) {//do something} if( !$.trim( $('#element').html() ).length ){//do something}

Last method will remove whitespace and check for the length of the content.


<!DOCTYPE html>
<script src=""> </script>
<title>Check div is empty or not using jQuery</title>
<style type="text/css">
.check{border:1px solid;height: 30px;width: 150px;float: left;} 
<div class="check" id="divID" ></div>
if(!$.trim( $('#divID').html()).length ) {
alert("Div is empty");
else {
alert("Div is not empty ");