Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect enter key pressed using javascript [duplicate]

Possible Duplicate:
Javascript capture key

I'm working on reply function using mysql and php.

My MySQL db:

reply:   rid - id;   topicid - under which topic;   uid - id of the guy who replies;   content - what did the guy said. 

Here's my html code:

<form id="replyform">   <textarea name="rplcont" placeholder="Press enter to submit your reply."><textarea> </form> 

My question is: How do I know if enter button is pressed?

Many thanks.

/----------------------------------------------/

here's my codes that work:

html:

<textarea id="reply" name="reply" onKeyPress="enterpressalert(event, this)"></textarea> 

js:

function enterpressalert(e, textarea){ var code = (e.keyCode ? e.keyCode : e.which); if(code == 13) { //Enter keycode  alert('enter press'); } } 

and it works.

Thanks for everyone who concerns this topic!

like image 400
Matt Avatar asked Jan 10 '13 05:01

Matt


People also ask

How do you detect a Enter key pressed in JS?

Using JavaScript In plain JavaScript, you can use the EventTarget. addEventListener() method to listen for keyup event. When it occurs, check the keyCode 's value to see if an Enter key is pressed.

How do you detect enter press?

To check whether user pressed ENTER key on webpage or on any input element, you can bind keypress or keydown event to that element or document object itself. Then in bind() function check the keycode of pressed key whether it's value is 13 is not.

How do you trigger button click on Enter key press using 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.


1 Answers

var code = (e.keyCode ? e.keyCode : e.which); if(code == 13) { //Enter keycode     alert('enter press'); } 
like image 191
Arun Killu Avatar answered Oct 08 '22 16:10

Arun Killu