How to check if element has HTML in it before removing? jQuery
html
is a method not a property, you need to use ()
, then you can use length
property of String object for checking the length of the returned html string:
if ( $.trim( $('.'+rowName+' div').html() ).length ) {
// $('.'+rowName).find('div, p, span').remove();
$('.'+rowName).find('div, p, span').empty();
}
Note that if you want to change the HTML content of an element, there is no need to remove the current html content of it. html
method overrides the current html content.
Check the children length.
if($('.'+rowName+' div').children().length > 0)
You can use .html()
for this (and accounting for whitespace):
var $row = $('.rowName');
if (!$row.find('div').html().trim().length) {
$row.find('div, span, p').empty();
}