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

72
Vistas
JavaScript enter Key

I have made a simple code that changes the heading when you input text in the input, but I want the event to trigger when I press the enter key as I have to press the button to get it to work, is there a piece of code I can implement to have his work? cheers.

function block() {
  var myValue = document.getElementById('textBox').value;
  if (myValue.length == 0) {
    alert('Please fill with valid text');
    return;
  }
  var myTitle = document.getElementById('Heading');
  myTitle.innerHTML = myValue;
};
<!DOCTYPE html>
<html>
  <head>
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Raleway:wght@100&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="fundamentals.css">
    <script src="fundamentals.js"></script>
    <title>Javscript</title>
  </head>
  <body>
    <h1 id="Heading">This is a heading</h1>
    <input type="text" id="textBox">
    <input type="submit" value="Click Me" onclick="block()">
  </body>
</html>

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

0

Wrap your input in a form element, so Enter triggers a submit event:

document.getElementById('heading-changer').addEventListener('submit', function (event) {
  event.preventDefault(); // otherwise page reloads
  var myValue = document.getElementById('textBox').value;
  if (myValue.length == 0) {
    alert('Please fill with valid text');
    return;
  }
  var myTitle = document.getElementById('Heading');
  myTitle.innerHTML = myValue;
});
<h1 id="Heading">This is a heading</h1>
<form id="heading-changer">
  <input type="text" id="textBox">
  <input type="submit" value="Click Me">
</form>

Or you can do this:

function handleKeyPress(e){
    if(e.keyCode === 13){
        e.preventDefault(); 

        var myValue = e.target.value;
        if (myValue.length == 0) {
          alert('Please fill with valid text');
          return;
        }
        
        var myTitle = document.getElementById('Heading');
        myTitle.innerHTML = myValue;
    }
}
<h1 id="Heading"></h1>
<input onkeypress={handleKeyPress(event)} />

about 4 years ago · Juan Pablo Isaza Denunciar

0

Add an eventListener to the input that is fires when keypressed, it the key is enter, then tha function is the same, as you made.

I found this one on W3Schools

var myInput = document.getElementById("textBox");
var myTitle = document.getElementById("Heading");

myInput.addEventListener("keypress", function (event) {
  if (event.key === "Enter") {
    if (myInput.value.length == 0) {
      alert("Please fill with valid text");
      return;
    }
    event.preventDefault();
    myTitle.innerHTML = myInput.value;
  }
});
<!DOCTYPE html>
<html>
  <head>
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Raleway:wght@100&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="fundamentals.css">
    <script src="fundamentals.js"></script>
    <title>Javscript</title>
  </head>
  <body>
    <h1 id="Heading">This is a heading</h1>
    <input type="text" id="textBox">
  </body>
</html>

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