Check if element has any children?

Viewed 23300

I am trying to remove an element from the DOM if it does not contain any elements.

Here is what I tried:

var numChildren = 0;
$("#messages").children().each(function () {
    numChildren += 1;
});
if (numChildren <= 0) {
    $("#messages").slideUp("normal", function () { $(this).remove(); });
}
2 Answers
Related