There is a block, inside which there is text and other blocks, and other elements. It is necessary to display the message "block is empty" if there is nothing inside it.

var uscont = $('.user-profile').html(); if (uscont !== "") { alert(1); } else { alert('Блок пуст'); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> <div class="user-profile">erfregrt4grtghrthg gbtrghrthtr htrhtrhtrhtrhrt <div class="profile"> d3f4rfg45g45g gfr4g45g54 g45g45g54g <div class="file"> d3f4rfg45g45g gfr4g45g54 g45g45g54g </div> </div> </div> 

in my code, regardless of whether there are any elements in the block or not, it still outputs 1. I noticed that even line breaks are taken into account. And if you delete everything from the parent block and put the closing tag on the same line, then everything works.

So how do you check the parent block for content inside, not paying attention to spaces and line breaks?

  • You do not have the right layout - Serge Esmanovich

2 answers 2

It’s just necessary to remove spaces and newline characters, in uscont.trim().length != 0 should be uscont.trim().length != 0

    $('.user-ptofile').children().length will show if there is someone inside