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

193
Visualizações
html show hide div element

I'm trying to show/hide div in html now I'm using w3schools Toggle Hide and Show link. It is working good but by default it shows condition I what by default hide condition if I press show button it should show. help me.

code

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
#myDIV {
  width: 100%;
  padding: 50px 0;
  text-align: center;
  background-color: lightblue;
  margin-top: 20px;
}
</style>
</head>
<body>

<p>Click the "Try it" button to toggle between hiding and showing the DIV element:</p>

<button onclick="myFunction()">Try it</button>

<div id="myDIV">
This is my DIV element.
</div>

<p><b>Note:</b> The element will not take up any space when the display property set to "none".</p>

<script>
function myFunction() {
  var x = document.getElementById("myDIV");
  if (x.style.display === "none") {
    x.style.display = "block";
  } else {
    x.style.display = "none";
  }
}
</script>

</body>
</html>
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You need to make the following changes in your code. Add a display:none for #myDIV in css to hide it on initial loading. But if you still check in JS (x.style.display), you will get an empty string instead of none. That's beacause the style attribute does not have any setting for display until the code is run through the first time. So to handle this case you need to update your if condition as well like this - if (x.style.display == "none" || x.style.display === "")

You can check here for more info - Similar Question Link

I have attached a snippet. Hope that's how you want it to work.

function myFunction() {
  var x = document.getElementById("myDIV");
  if (x.style.display == "none" || x.style.display === "") {
    x.style.display = "block";
  } else {
    x.style.display = "none";
  }
}
#myDIV {
  width: 100%;
  display: none;
  padding: 50px 0;
  text-align: center;
  background-color: lightblue;
  margin-top: 20px;
}
<p>Click the "Try it" button to toggle between hiding and showing the DIV element:</p>

<button onclick="myFunction()">Try it</button>

<div id="myDIV">
This is my DIV element.
</div>

<p><b>Note:</b> The element will not take up any space when the display property set to "none".</p>

about 4 years ago · Juan Pablo Isaza Relatório

0

Add display: none to the div css,

#myDIV {
  width: 100%;
  padding: 50px 0;
  text-align: center;
  background-color: lightblue;
  margin-top: 20px;
  display: none;
}

and change the if condition to this:

if (x.style.display == "none" || x.style.display === "")
about 4 years ago · Juan Pablo Isaza Relatório

0

I suggest do not update the style property directly, but toggle a class "hidden" instead. Here is a snippet:

function myFunction() {
  const x = document.getElementById("myDIV");
  x.classList.toggle("hidden");
}
#myDIV {
  width: 100%;
  padding: 50px 0;
  text-align: center;
  background-color: lightblue;
  margin-top: 20px;
}

.hidden {
  display: none;
}
<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
  </head>
  <body>

    <p>Click the "Try it" button to toggle between hiding and showing the DIV element:</p>

    <button onclick="myFunction()">Try it</button>

    <div id="myDIV" class="hidden">
      This is my DIV element.
    </div>
  </body>
</html>

If this does not solve your problem, please let me know ;)

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