I'm trying to check in jQuery if an element with a certain class contains some text. If it doesn't contain any text, then I want to remove the class.
<div class="description">
</div>
I'm trying to check in jQuery if an element with a certain class contains some text. If it doesn't contain any text, then I want to remove the class.
<div class="description">
</div>
Use :empty
$('.description:empty').removeClass('description');
According to documentation
Select all elements that have no children (including text nodes).
console.log($('.description:empty').length);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<div class="description"></div>
However, if there is poorly formed HTML as in the question(unnecessary whitespace), you will have to take a more manual approach
if ($(this).children().length == 0 && $(this).text().trim() == '')
$('.description').each(function() {
if ($(this).children().length == 0 && $(this).text().trim() == '') {
console.log('entered');
$(this).removeClass('description');
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<div class="description">
</div>
if(!$('.description').text().length)
$('.description').hide();
if ($('.description').text().trim()=="") {
$(this).hide();
else {
/*non-empty actions */
}
Try this syntex
if($('.description').innrHTML=='')
{
$('.description').hide()
}