Snippets → jQuery Code Snippets → Check for Empty Elements Check for Empty Elements Chris Coyier on Oct 16, 2012 Do something for each empty element found: $('*').each(function() { if ($(this).text() == "") { //Do Something } }); TRUE or FALSE if element is empty: var emptyTest = $('#myDiv').is(':empty');
This might be a bit more practical
$(‘p,em,b,strong,span’).each(function() {
if (jQuery.trim ($(this).text()) == “”) $(this).css(‘border’,’solid 1px red’);
});
Thanks Cipa!
Hey Chris, I think you’ve missed the single quotes around :empty
.is(‘:empty’)
Not true, how about ”
“?It’s not empty, but it hasn’t text. I think checking childNodes property is better.
$(‘*’).each(function() {
if (!this.childNodes.length) {
//Do Something
}
});
Contents of my previous reply in the double quotes are filtered, here it is:
<div><img/></div>
Thank you Cipa! I think that trim makes a difference.
nice …