Run method when enter is pressed

Dan

I am coding a webpage that has a form with one field where a user types in a set of numbers then press a button that says 'Run' to run the code, the problem is when you press the 'enter' key instead of the 'Run' button it doesn't do anything. It is suppose to run a javascript method called 'calculate()'.

I tried looking for a method called 'onEnter' or 'onSubmit' but couldn't find any.

Sivcan Singh

You can attach a listener on the keypress in the body / some specific element. The key code for enter is 13, so you can listen to that.

let element = document.getElementById('#some-element');

element.addEventListener('keydown', (e) => {
  e.preventDefault();
  if (e.keyCode === 13) {
    calculate();
  }
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Run javascript function when enter is pressed - JavaScript only no Jquery

Click a button on a modal, when the enter key is pressed, when the modal is shown

Disable input when enter is pressed dynamically

Submit a form when enter is pressed in a textarea in React?

Run a Macro in Excel 2010 using Enter Key, Whether Worksheet has Changed or Not, when Pressed in Particular Range

Java Scanner not accepting input when enter pressed

Move + Submit Input when enter is pressed

Javascript to trigger submit when enter is pressed

Make a form run a javascript when ENTER is pressed or its button is pressed, without submitting

Invoke a serie of actions when Enter key is pressed

add characters when enter button pressed

Which method is run when Home button pressed?

When Enter is pressed Ok Button should be clicked

Stop entering values into array when enter is pressed

How to go to next textbox when enter is pressed?

Run Javascript when enter pressed

Cancel Instant Search when Enter is pressed

Perform search only when enter is pressed for UISearchController

Call a javascript function when enter key is pressed

Writing to a div from a form when enter is pressed

Call specific method in javascript when enter key pressed anywhere in page

jQuery: content to show when enter key is pressed

Submit a certain button when ENTER is pressed

Form not submitting when enter key is pressed

Odd RubyMine indenting when enter is pressed

JavaFX run a method once a specified key is pressed

How to show alert when enter key pressed

Submit a form only when 'enter' key is pressed

Android remove focus of TextEdit when Enter is pressed