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.
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.
Comments