Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use the Enter key as an event handler (javascript)? [duplicate]

im trying to make my own chat... so i have an input text field, the submit button, isn't even submit, its just a button.... so when the enter key is pressed, i need the value of the input field to appear in my textarea (which is readonly)...

well look.. make long story short, i just want a basic enter key event handler, i know it works perfectly with submit buttons cus you don't need to program anything at all, its default. but my button is type="button" .... so when you press enter nothing happens... how do i trigger my button by pressing enter?

like image 734
Juan Veliz Avatar asked May 09 '11 00:05

Juan Veliz


People also ask

How do you trigger a function when enter key is pressed in JavaScript?

To trigger a click button on ENTER key, We can use any of the keyup(), keydown() and keypress() events of jQuery. keyup(): This event occurs when a keyboard key is released. The method either triggers the keyup event, or to run a function when a keyup event occurs.

How do you call a function on enter key?

You can execute a function by pressing the enter key in a field using the key event in JavaScript. If the user presses the button use the keydown to get know its enter button or not. If it enters the key then call the JavaScript function.

How do you capture enter key press event in typescript?

addEventListener("keyup", (event) => { if (event. key === "Enter") { console. log('Enter key pressed') } });

What is e keycode === 13?

key 13 keycode is for ENTER key.


1 Answers

Here is a version of the currently accepted answer (from @entonio) with key instead of keyCode:

HTML:

<input onkeyup="inputKeyUp(event)" ...>

Plain javascript:

function inputKeyUp(e) {
    if (e.key === 'Enter') {
        // submit
    }
}
like image 78
Marcus Avatar answered Oct 23 '22 13:10

Marcus