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

225
Visualizações
How can I show a hidden text by clicking on a button?

function myFunction() {
  var x = document.getElementById("myDIV");
  if (x.style.display = "none") {
    x.style.display = "block";
  } else {
    x.style.display = "none";
  }
}
<ul>
  <li><button onclick="myFunction()">Try it</button>
    <div id="myDIV"><p>This is my DIV element.</p></div 
  </li>
</ul>

In the css file I included the command: #myDIV{ display: none; } because otherwise the text would not be hidden at first. With this code the text is hidden at first and if I click on the button it shows the text. But then if I click on the button again it should be closing the text which it does not. Can someone help me out here? Thanks!

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

0

Set this to HTML

<div id="myDIV" style="display: none;"><p>This is my DIV element.</p></div>

and this javascript

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

This now will work because the display none isn't by default to div element so javascript will not see the display none style and will not execute the function. So you need to specify style display none and the function will work correctly

about 4 years ago · Juan Pablo Isaza Relatório

0

= is assignment. == and === are comparison. So inside of the if, change it to one of those two.

You also didn't properly close the <div> in the code you submitted.

Another thing is that you're setting the display: none in a CSS file. JavaScript doesn't have access to this value, it only has access to style="..." in the HTML markup of the element. To fix this issue, check if it's not hidden and swap around the sides of the if/else block.

function myFunction() {
  var x = document.getElementById("myDIV");
  if (x.style.display == "block") { // `=` to `==` or `===`
    x.style.display = "none";
  } else {
    x.style.display = "block";
  }
}
#myDIV {
  display: none;
}
<ul>
  <li><button onclick="myFunction()">Try it</button>
    <div id="myDIV">
      <p>This is my DIV element.</p>
    </div> <!-- this div wasn't closed properly -->
  </li>
</ul>

about 4 years ago · Juan Pablo Isaza Relatório

0

function myFunction() {
  var x = document.getElementById("myDIV");
  x.style.display = x.style.display != "block"?"block":"none"
}
#myDIV{ display: none; } 
<ul>
  <li><button onclick="myFunction()">Try it</button>
    <div id="myDIV"><p>This is my DIV element.</p></div 
  </li>
</ul>

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