This is my scenario:
<div><br><div>
<div>
line one.
<br>
line two.
</div>
<div><img src="example.jpg"></div>
<div><br></div>
<div><iframe></iframe></div>
<div><br></div>
I need to check if the div contains NO text, as well as the br tag and removes it.
I tried this:
if ($('div').is(':empty')) {
$('div').remove();
}
This did not work, I think, because empty () does not mean "no text."
I also tried this:
$('div').filter(function() {
return $.trim($(this).text()) === '';
}).remove();
This worked, however, it removed all div tags containing only text, so all my images and any other html tag inside the div were deleted.
The last thing I tried was this, but then I realized that this does not take into account the text that may be in the div.
$('div br:only-child').remove();
I am still new to jquery and apologize for any annoyance that I can cause by asking y'all for help. Thank you in advance!!
source
share