Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

189
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda