title: How to find if an HTML element is empty or not?
body: <p>I want to find out if an element is empty or not. Probably this is one of the best examples to help people understand <strong>difference between innerText and innerHTML</strong> as well.</p>
  
  <p>Here are the examples:</p>
  
  <pre><code>1. &lt;div&gt; &lt;!-- Just a comment node is present inside a div --&gt; &lt;/div&gt;
  
  2. &lt;div&gt; &lt;span&gt;&lt;/span&gt; &lt;/div&gt;
  
  3. &lt;div&gt; hi &lt;/div&gt;
  
  4. &lt;div&gt; hello &lt;!-- world --&gt; I know javascript &lt;/div&gt;
  
  5. &lt;div&gt;    &lt;/div&gt;
  </code></pre>
  
  <p><strong>Example_Number |  innerHTML |  innerText | #childElements | isElementEmpty(Result)</strong></p>
  
  <p>1............................|  Not Empty....| Empty........| 0.........................| YES</p>
  
  <p>2............................| Not Empty....| Empty........| 1.........................| No</p>
  
  <p>3............................| Not Empty....| Not Empty..| 0.........................| No</p>
  
  <p>4............................| Not Empty....| Not Empty..| 0.........................| No</p>
  
  <p>5............................| Empty..........| Empty.........| 0.........................| Yes</p>
  
  <p><em>In #5, trimmed value is considered.</em></p>
  
  <p>Clearly, <strong>innerHTML does not contribute</strong> to check whether an element is empty or not. Now, we need to check how innerText/textContent and numberOfChildElement contribute. So based on above findings, I have concluded</p>
  
  <p>An element is empty when both of these conditions is met.</p>
  
  <ol>
  <li>Trimmerd innerText/textContent is empty. (Satisfies #5)</li>
  <li>Number of child elements is ZERO. (Satisfies #2)</li>
  </ol>
  
  <p>So the code becomes</p>
  
  <pre><code>function isEmpty(element) {
    return (element.textContent.trim() &amp;&amp; element.childElementCount == 0);
  }
  </code></pre>
  
  <p>I just want to validate my approach and tell if I miss any use case here or a better solution would be really helpful.</p>