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

74
Views
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 answers
Answer question

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 Report

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 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!