View Full Version : Onkey enter keypress

01-17-2009, 02:43 AM
I want to have an event handler where if the "enter" key is pressed while in a specified text field . run a function for example.
say if i have

<input type="text" id="textfield">

They type in the field whatever they want. and as soon as the "Enter" button is pressed (i belive enter is 13 or 10) , alert the value of the text field

Philip M
01-17-2009, 08:26 AM
Here you are:-

<input type = "text" id = "textfield" size = "20" onkeyup = "chk(event)">

<script type = "text/javascript">

function chk(ev) {
var key;
ev = ev || event;
key = ev.keyCode;
if (key == 13) {
var val = document.getElementById("textfield").value;
alert ("You pressed the Enter key\nYou have entered " + val);


"A psychiatrist asks a lot of expensive questions that your wife asks for nothing." – Joey Adams

01-17-2009, 08:33 AM
You beat me Philip, but one thing:

<input type = "text" id = "textfield" size = "20" onkeyup = "chk(event)">

...will make it work on FF too.