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

Viewed 578435

I'm trying to call a function only if an HTML element is empty, using jQuery.

Something like this:

if (isEmpty($('#element'))) {
    // do something
}
18 Answers

Vanilla javascript solution:

if(document.querySelector('#element:empty')) {
  //element is empty
}

Keep in mind whitespaces will affect empty, but comments do not. For more info check MDN about empty pseudo-class.

Related