Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

234
Visualizações
How can I check whether innerHTML is empty?

so i am trying to set a Class on a Parent Element (Section), when the second Child Div innerHTML is empty.

So for example: (This Section should get a class of "hide-section" to hide it)

<section class="ef-area">
   <div class="child-1">
       <div class="child-2">
       </div>
   </div>
</section>

This one should not be hidden because "child-2" is not empty

<section class="ef-area">
   <div class="child-1">
      <div class="child-2">
         <div class="child-3">
            ...//What Ever
         </div>
      </div>
  </div>
</section>

I looped over all "ef-area" sections but how can i set only those sections to display = none when the second child (child-2) is empty.

What I did is:

    let efAreaDivs = document.querySelectorAll(".ef-area > div > div");
  
    for (singleDiv of efAreaDivs) {
    
    if (singleDiv.innerHTML == "") {
      singleDiv.closest("section").classList.add("hide-section");
    }

The class "hide-section" never gets set.

I think js always ignores it, because there are singleDiv´s that are not empty or am I wrong?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The content of <div class="child-2"> below

<section class="ef-area">
   <div class="child-1">
       <div class="child-2">
       </div>
   </div>
</section>

is NOT an empty string ... it is "\n "

You need to trim the innerHTML (which get's rid of whitspace at the beginning and end of the string, in this case everything)

if (singleDiv.innerHTML.trim() == "") {
   singleDiv.closest("section").classList.add("hide-section");
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda