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

153
Vistas
How to onclick check password (form) and if correct show hidden div on same page without reloading?

I've been trying to have a password only form that shows a hidden div on the same page as the form when the password is correct. I don't want to reload either but not sure how to do that.

function checkPswd() {
  var confirmpassword = "admin";
  var password = document.getElementById("pass").value;
  if (password == confirmpassword) {
    document.getElementById('hidden');
    if (x.style.display === "none") {
      x.style.display = "block";
    } else {
      alert('tryagain');
      x.style.display = "none";
    }
  };}
.hidden {
  display: none;
}
<form method="post">
  <label for="pswd">enter password</label>
  <input type="password" id="pass">
  <input type="button" value="go" onsubmit="checkPswd();" /></form>
<div class="hidden">
  <p>this part I want hidden from people who don't know the password</p>
</div>

I'm pretty sure the javascript is wrong but is there another way to do this?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Few things:

  1. You forgot to assign the returned value of document.getElementById('hidden') to x

  2. A button does not have a submit event. You are perhaps looking for the click event

  3. There is no element with the hidden id. You are mixing it up with the element with the hidden class.

function checkPswd() {
  var confirmpassword = "admin";
  var password = document.getElementById("pass").value;
  if (password == confirmpassword) {
    var x = document.querySelector('.hidden');
    if (x.style.display === "none") {
      x.style.display = "block";
    } else {
      alert('tryagain');
      x.style.display = "none";
    }
  };
}
.hidden {
  display: none;
}
<form method="post">
  <label for="pswd">enter password</label>
  <input type="password" id="pass">
  <input type="button" value="go" onclick="checkPswd();" /></form>
<div class="hidden">
  <p>this part I want hidden from people who don't know the password</p>
</div>

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