38

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!

Community
  • 1
  • 1
Matt
  • 565
  • 1
  • 7
  • 12

1 Answers1

64
var code = (e.keyCode ? e.keyCode : e.which);
if(code == 13) { //Enter keycode
    alert('enter press');
}
Mathew Berg
  • 28,625
  • 11
  • 69
  • 90
Arun Killu
  • 13,581
  • 5
  • 34
  • 61
  • thanks for reply! I edit your code a little bit.. function doit(e, textarea){ ... // your code } – Matt Jan 10 '13 at 07:22
  • 13
    `e.keyCode` is now deprecated. Mozilla recommends using `if (e.code === "Enter")`, even though Microsoft browsers lack support. https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/code – Tomaso Albinoni Nov 16 '19 at 18:25