Home Articles FAQs XREF Games Software Instant Books About Feedback Search Site-Map
irt.org logo

Q985 How can I detect the enter key being pressed in an text input field?

irt.org | Knowledge Base | JavaScript | Event | Q985 [ previous next ]

Q985 How can I detect the enter key being pressed in an text input field?

Try:

<html>

<head>

<script language="JavaScript1.2"><!--
function netscapeKeyPress(e) {
    if (e.which == 13)
        alert('Enter pressed');
}

function microsoftKeyPress() {
    if (window.event.keyCode == 13)
        alert('Enter pressed');
}

if (navigator.appName == 'Netscape') {
    window.captureEvents(Event.KEYPRESS);
    window.onKeyPress = netscapeKeyPress;
}
//--></script>

</head>

<body onKeyPress="microsoftKeyPress()">

<form name="test">
<textarea></textarea>
<input type="text">
</form>

</body>

</html>

Only the textarea appears to work on Netscape Navigator 4.5 on GNU/Linux.

The onBlur event handler can be used to detect when a field loses focus:

<form>
<input type="text" onBlur="alert('Enter *might* have been pressed')">
</form>

Feedback on 'Q985 How can I detect the enter key being pressed in an text input field?'


Provide feedback ...
AddThis Social Bookmark Button

Provide feedback ... AddThis Social Bookmark Button


Last Updated: 6th July 2009. Maintained by: Martin Webb and Michel Plungjan
irt.org liability, trademark, document use, privacy statement and software licensing rules apply.
Copyright © 1996-2009 irt.org, All Rights Reserved.