Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

480
Views
¿Cómo puedo llamar a una función cuando presiono la tecla enter?

Estoy creando una contraseña para mi sitio web. ¿Cómo puedo verificar si la contraseña es correcta presionando la tecla Intro en lugar de presionar el botón Enviar? La ayuda sería apreciada.

 <html> <script> function checkPassword() { var password = document.getElementById("passwordBox"); var passwordText = password.value; if(passwordText == "password") { alert("you got it right"); return true; } alert("you got it wrong"); return false; } </script> </head> <body> <p style="font-size: 30pt;">something</p> <p>Please enter the password to uh... something </p> <p>Password: <input id="passwordBox" type="password"/></p> <button onclick="return checkPassword();"> Submit </button> </html>
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

envuelva su entrada y el botón en la etiqueta <form> y use el oyente de envío, no necesita escribir en clic e ingresar clic uno por uno, será un gran lío en el código, en el elemento del botón ise type="submit" , lo que significa que es parte del form y ejecutará la función si hace clic en entrar o en el botón, también eliminé devolver true y false porque no tiene ningún sentido en el código, también lea sobre qué es e.preventDefult()

 const passInput = document.getElementById("passwordBox") const form = document.getElementById("form") form.addEventListener("submit", (e) => { e.preventDefault() var passwordText = passInput.value; if(passwordText == "password") { alert("you got it right"); }else alert("you got it wrong"); })
 <p style="font-size: 30pt;">something</p> <p>Please enter the password to uh... something </p> <form id="form"> <p>Password: <input id="passwordBox" type="password" /></p> <button type="submit">Submit</button> </form>

about 4 years ago · Juan Pablo Isaza Report

0

La forma más sencilla de manejar la pulsación de la tecla enter es envolverla dentro de un formulario y configurar onSubmit en el formulario y type="submit" en el botón.

Aquí está el ejemplo.

 <html> <head> <script> function checkPassword() { var password = document.getElementById("passwordBox"); var passwordText = password.value; if (passwordText == "password") { alert("you got it right"); return true; } alert("you got it wrong"); return false; } </script> </head> <body> <p style="font-size: 30pt">something</p> <p>Please enter the password to uh... something</p> <form onsubmit="checkPassword()"> <p>Password: <input id="passwordBox" type="password" /></p> <button type="submit">Submit</button> </form> </body> </html>
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!