How do you hide a div when it’s empty?

Asked

Viewed 1,182 times

-1

I have a div to show alerts to the user when a new user is successfully registered, but I only intend to show this message in the div, IE, the div when it is not empty.

How do I do?

Note: I saw an example using Thymeleaf but it does not work, IE, the namespace does not recognize in the project.

Someone there to help?

  • 1

    You can explain how this page is loaded and what is the logic to fill content in that div, when the user is registered?

  • I think there’s a tag mix-up there. Java-ee and Java have nothing to do with the subject, maybe you’re confusing it all as Javascript.

3 answers

4

If I understand your question, you can only use css to solve this issue. It is possible to 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>

  • Friend @Renan all right? I want to show a message from Success when a new user is registered, it happens that this message will be shown in a div, so when I am in the registration form the div has to disappear and when saved a new user it has to appear with the following message, "User saved successfully".

  • That’s what I answered, just put this message inside a div with this rule I put in the reply. As you did not explain how you make this message appear, there is not much more to answer.

  • 1

    +1 For the use of the pseudo-class

0

That’s a vague 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 the normal behavior of a DIV is to remain invisible when without content.

  • So far in HTML I understood, now in Javascript I do not understand well, I have a variable "message" initially it must be empty and when saved a new record it gets content, message="Saved successfully" and show the div with this message @user5978.

0


As already said by some, 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");

Browser other questions tagged

You are not signed in. Login or sign up in order to post.