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

386
Visualizações
Why does my text briefly show up and then suddenly becomes invisible?

I have a button that, when clicked, reveals hidden text by changing its CSS display property to "block". However, when I click the button, the text briefly shows up and then immediately disappear afterwards. How do I fix this?

const contactPopUp = document.getElementById("contactButton");
const exitButton = document.getElementById("exit");

function showPopUp() {
  document.getElementById("contacts").style.cssText =
    "display: block; position: absolute; top: 30%; right: 40%; background-color: rgb(0, 0, 119); font - family: verdana, sans - serif; color: white; padding: 30 px;";
}

function closePopUp() {
  document.getElementById("contacts").style.display = "none";
}

contactPopUp.addEventListener("click", showPopUp);
exitButton.addEventListener("click", closePopUp);
#contacts {
  display: none;
  position: absolute;
  top: 30%;
  right: 40%;
  background-color: rgb(0, 0, 119);
  font-family: verdana, sans-serif;
  color: white;
  padding: 30px;
}

#contact_list {
  display: block;
  font-family: verdana, sans-serif;
  color: white;
}
<div id="header_container">
  <ul>
    <li id="homepage_name">text</li>
    <li><a href="">Home</a></li>
    <li><a href="">About us</a></li>
    <li><a id="contactButton" href="">Contacts</a></li>
  </ul>
</div>

<div id="contacts">
  You may contact us at: <button id="exit">X</button>
  <ul>
    <li id="contact_list">Insert Contacts Here.</li>
    <li id="contact_list">Insert Contacts Here.</li>
    <li id="contact_list">Insert Contacts Here.</li>
  </ul>
</div>

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

0

The problem is that href="" reloads the page. You may want to use href="#" or make the button an actual button.

const contactPopUp = document.getElementById("contactButton");
const exitButton = document.getElementById("exit");

function showPopUp() {
  document.getElementById("contacts").style.cssText =
    "display: block; position: absolute; top: 30%; right: 40%; background-color: rgb(0, 0, 119); font - family: verdana, sans - serif; color: white; padding: 30 px;";
}

function closePopUp() {
  document.getElementById("contacts").style.display = "none";
}

contactPopUp.addEventListener("click", showPopUp);
exitButton.addEventListener("click", closePopUp);
#contacts {
  display: none;
  position: absolute;
  top: 30%;
  right: 40%;
  background-color: rgb(0, 0, 119);
  font-family: verdana, sans-serif;
  color: white;
  padding: 30px;
}

#contact_list {
  display: block;
  font-family: verdana, sans-serif;
  color: white;
}
<div id="header_container">
  <ul>
      <li id="homepage_name">text</li>
      <li><a href="">Home</a></li>
      <li><a href="">About us</a></li>
      <li>
      <a id="contactButton" href="#">Contacts</a>
      <!-- <button id="contactButton">Contacts</a> -->
      </li>
  </ul>
  
   <div id="contacts">
  You may contact us at: <button id="exit">X</button>
  <ul>
      <li id="contact_list">Insert Contacts Here.</li>
      <li id="contact_list">Insert Contacts Here.</li>
      <li id="contact_list">Insert Contacts Here.</li>
  </ul>

about 4 years ago · Juan Pablo Isaza Relatório

0

This happens because when you click on the Contacts button, the page reloads. And this happens because the A tag surrounding your button has the empty href="" attribute, which tells the site to reload the page when the button is clicked, you can fix this by adding href="#". Also rewrote your code a bit, besides the div tag with id="header_container" didn't have a closing tag.

 #contacts {
   display: none;
 }
 #contacts._active {
   display: block; 
   position: absolute; 
   top: 30%; 
   right: 40%; 
   background-color: rgb(0, 0, 119); 
   font-family: verdana, sans-serif; 
   color: white; 
   padding: 30px;
 }
 
 #contact_list {
   display: block;
   font-family: verdana, sans-serif;
   color: white;
 }
</style>
 
<div id="header_container">
 <ul>
   <li id="homepage_name">text</li>
   <li><a href="">Home</a></li>
   <li><a href="">About us</a></li>
   <li><a id="contactButton" href="#">Contacts</a></li>
 </ul>
 <div id="contacts">
   You may contact us at: <button id="exit">X</button>
   <ul>
     <li id="contact_list">Insert Contacts Here.</li>
     <li id="contact_list">Insert Contacts Here.</li>
     <li id="contact_list">Insert Contacts Here.</li>
   </ul>
 </div>
<script>
 const contactPopUp = document.getElementById("contactButton");
 const exitButton = document.getElementById("exit");
   
 contactPopUp.addEventListener("click", function() {
   document.getElementById("contacts").classList.add("_active");
 });
 exitButton.addEventListener("click", function() {
   document.getElementById("contacts").classList.remove("_active");
 });
</script>
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