How do I check if an html element is empty using jQuery?

In this tutorial, we'll discuss whether is it possible to check if an HTML element is empty or not using jQuery?
Submitted by Pratishtha Saxena, on July 24, 2022

jQuery :empty Selector

This selector of jQuery returns the result 'true' if the selected element is empty and 'false' if the element is not empty. Generally, the empty selector is used along with is() method for implementation. No selector can be used on its own. It is always paired up with some other method.



Here, the role of is() method is to check the selected element to the appropriate selector used.

This way of checking an empty element is very easy. This empty selector not only returns true if the element is empty but also if the element has no children further.

Let's implement this and understand using an example.



<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <script src=""></script>
      text-align: center;
    <div id="box1">
      <b>Div 1:</b>
      <div id="Div_1">Hey guys !!!</div>
      <button id="bt_1">Click to Check</button>
    <div id="box2">
      <b>Div 2:</b>
      <div id="Div_2"></div>
      <button id="bt_2">Click to Check</button>


        $("#box1").css("background", "yellow");
        if ($('#Div_1').is(':empty')){
          console.log("This div is Empty.");
          console.log("This div is not Empty.");
        $("#box2").css("background", "yellow");
        if ($('#Div_2').is(':empty')){
          console.log("This div is Empty.");
          console.log("This div is not Empty.");


Example 1: Check if an html element is empty

Example 2: Check if an html element is empty


Comments and Discussions!

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© some rights reserved.