How to check an HTML element is empty using jQuery ?
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:
<!DOCTYPE html> <html> <head> <title> How to check an HTML element is empty using jQuery ? </title> <script src= </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:
<!DOCTYPE html> <html> <head> <title> How to check if an HTML element is empty using jQuery? </title> <script src= </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:

Recommended Posts:
- How to check input file is empty or not using JavaScript/jQuery ?
- How to check an element is visible or not using jQuery?
- How to check an element contains a class using jQuery?
- How to check if an element is hidden in jQuery?
- How to check an element has certain CSS style using jQuery ?
- How to change an HTML element name using jQuery ?
- How to select html element using ID in jQuery when ID contains a dot character ?
- jQuery | :empty Selector
- jQuery | empty() with Examples
- Check whether HTML element has scrollbars using JavaScript
- How to remove parent element except its child element using jQuery ?
- How to check whether an array is empty using PHP?
- Why to check both isset() and !empty() function in PHP ?
- How to check an object is empty using JavaScript?
- Check if an array is empty or not in JavaScript
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


