Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org
#

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

You are here: irt.org | FAQ | JavaScript | Event | Q985 [ previous next ]

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?'

©2018 Martin Webb