|
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?'
|
|
Copyright © 1996-2008 irt.org, All Rights Reserved.