How to check an HTML element is empty using jQuery ? Last Updated : 03 Aug, 2021 Comments Improve Suggest changes Like Article Like Report Given an HTML document and select an element from the HTML document and check that element is empty or not. There are two methods used to solve this problem which are discussed below: Method 1: Using the ":empty" selector: The element to be checked using is() method. The is() method is used to check if one of the selected elements matches to the selector element. This method can be used on this element to test if it is empty by using ":empty" selector. The ":empty" selector is used to select all the elements that have no children. It will return true if the element is empty, otherwise, return false. Syntax: $('#elementSelector').is(':empty') Example: html <!DOCTYPE html> <html> <head> <title> How to check an HTML element is empty using jQuery ? </title> <script src= "https://code.jquery.com/jquery-3.3.1.min.js"> </script> <style> #empty { background-color: green; height: 50px; border: solid; } #not-empty { background-color: lightgreen; height: 50px; border: solid; text-align: center; } </style> </head> <body> <h1 style="color: green"> GeeksForGeeks </h1> <b> How to check if an HTML element is empty using jQuery? </b> <p> Click on the button to check if the paragraph elements are empty. </p> <p>The div below is empty.</p> <div id="empty"></div> <p>The div below is not empty.</p> <div id="not-empty">This element has something!</div> <p> First div is empty: <span class="output1"></span> </p> <p> Second div element is empty: <span class="output2"></span> </p> <button onclick="checkifEmpty()"> Check if empty </button> <script> function checkifEmpty() { if ($('#empty').is(':empty')) { document.querySelector('.output1').textContent = true; } else { document.querySelector('.output1').textContent = false; } if ($('#not-empty').is(':empty')) { document.querySelector('.output2').textContent = true; } else { document.querySelector('.output2').textContent = false; } }; </script> </body> </html> Output: Before clicking the button: After clicking the button: Method 2: Using the length property: The length property is used on this element to determine its length. It finds the number of object in a jQuery element. A length of 0 means the element has no other elements in it. Any value other than 0 means that some element is present. This can be used to check if an element is empty or not. Syntax: $('#elementSelector').length Example: html <!DOCTYPE html> <html> <head> <title> How to check if an HTML element is empty using jQuery? </title> <script src= "https://code.jquery.com/jquery-3.3.1.min.js"> </script> <style> #empty { background-color: green; height: 50px; border: solid; } #not-empty { background-color: lightgreen; height: 50px; border: solid; text-align: center; } </style> </head> <body> <h1 style="color: green"> GeeksForGeeks </h1> <b> How to check if an HTML element is empty using jQuery? </b> <p> Click on the button to check if the paragraph elements are empty. </p> <p>The div below is empty.</p> <div id="empty"></div> <p>The div below is not empty.</p> <div id="not-empty"> This element has something! </div> <p> First div is empty: <span class="output1"></span> </p> <p> Second div element is empty: <span class="output2"></span> </p> <button onclick="checkifEmpty()"> Check if empty </button> <script> function checkifEmpty() { if ($('#empty').length) { document.querySelector('.output1').textContent = true; } else { document.querySelector('.output1').textContent = false; } if ($('#non-empty').length) { document.querySelector('.output2').textContent = true; } else { document.querySelector('.output2').textContent = false; } }; </script> </body> </html> Output: Before clicking the button: After clicking the button: jQuery is an open source JavaScript library that simplifies the interactions between an HTML/CSS document, It is widely famous with it's philosophy of “Write less, do more". You can learn jQuery from the ground up by following this jQuery Tutorial and jQuery Examples. Comment More infoAdvertise with us Next Article How to check an HTML element is empty using jQuery ? sayantanm19 Follow Improve Article Tags : Web Technologies JQuery jQuery-Misc Similar Reads How to check if an element is hidden in jQuery? To check if an element is hidden or not, jQuery :hidden selector can be used. .toggle() function is used to toggle the visibility of an element. Syntax:$(element).is(":hidden");Example:html<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title> Jquery Hid 1 min read How to check an array is empty or not using jQuery ? In this article, we will check if an array is empty or not using jQuery. In JavaScript, arrays are a special type of object. If we use the typeof operator for arrays, it returns "object". We can use jQuery's isEmptyObject() method to check whether the array is empty or contains elements. The isEmpty 2 min read How to check element exists or not in jQuery ? Checking if an element exists in jQuery involves selecting the element and verifying its length. If the selected element's length is greater than 0, the element exists in the DOM; otherwise, it doesn't. This is useful for conditionally manipulating elements. There are two ways to check whether an el 3 min read How to check an element contains a class using jQuery? Method 1: Using hasClass() method: The hasClass() is an inbuilt method in jQuery which check whether the elements with the specified class name exists or not. It returns a boolean value specifying whether the class exists in the element or not. This can be used to check for multiple classes. Syntax: 2 min read How to count child element using jQuery ? It is very simple to count the number of child elements in the HTML file using jQuery. For example: If you have a parent element consisting of many children elements then you can use .length or.children().length method as given below. Syntax: var len=$("#parentID").length; or var count = $("#parentI 1 min read How to Check an Object is Empty using JavaScript? These are the following ways that can be used to Check an Object is Empty using JavaScript: 1. Using Object.keys() Method - Mostly usedThe Object.keys() method returns an array that contains the property names of an object. If the length of array is 0, then object is empty.JavaScriptlet obj = {}; if 2 min read How to empty the content of an element using AngularJS ? In this article, we will see how to remove the content of an element in HTML DOM using AngularJS. This task can be accomplished by implementing the jQuery empty() method that removes all child nodes and their content for the selected elements. Syntax: element.empty();Parameter: This method does not 2 min read How to finds all elements that are empty in jQuery ? In this article, we will see how to find all elements on the page that are empty using jQuery. Approach 1: The :empty selector can be used to get all elements in the page that are currently empty. These elements are iterated over using the each() method and can be accessed using the this reference i 3 min read How to Check an Element with Specific ID Exists using JavaScript ? Given an HTML document containing some elements and the elements contain some id attribute. The task is to check whether the element with a specific ID exists or not using JavaScript. Below are the approaches to check an element with specific ID exists or not using JavaScript: Table of ContentApproa 3 min read How to hide span element if anchor href attribute is empty using JavaScript ? The task is to hide the <span> tag when href attribute of <a> tag is not present or invalid. Let us consider we have <a> tag inside the <span> tag. It will look similar to this: <span><a href="www.google.com"></a></span> Now we add the id to the elemen 2 min read Like