How to hide a div when it is empty? [closed]

-1

How do I do it?

Note: I have seen an example using thymeleaf but it does not work, that is, the namespace does not recognize the project.

Someone there to help?

    
asked by anonymous 24.04.2016 / 07:15

3 answers

0

As some have said, make the div initially invisible. You can even leave the message already set in it:

CSS:

.divSucesso{ 
  display:none;
}

HTML:

<div id="divSucesso" class="divSucesso">Usuário salvo com sucesso</div>

When the user is saved, make this div appear, using JQuery:

$("#divSucesso").show();

If you need to hide the div again, use:

$("#divSucesso").hide();

If you need to change the displayed message, you can use JQuery again:

 $("#divSucesso").html("Nova mensagem");
    
27.04.2016 / 21:31
4

If I understand your question, you can only use css to solve this question. You can define specific rules for an element when it is empty through the pseudo-class :empty :

div {
  border : 2px solid #1abc9c;
  color  : #333;
  padding: 4px;
}

div:empty {
  display: none
}
<div><!-- DIV VAZIA --></div>
<div>Tenho conteúdo</div>
    
24.04.2016 / 13:46
0

Half a question, but according to your description of the problem:

CSS:

 .hide{ 
   display:none;
  }

Html:

 <div  class="_div"> </div>

JavaScript:

 ...
 if ($("._div").html()==""){
     $("._div").addClass("hide");
 }

Although normal behavior of a DIV is invisible when no content.

    
24.04.2016 / 08:44