hi pravinasar,
I don't need to disable tab & enter for every field, just certain ones. I was hoping for a method that would allow me to attach an event handler like so
<input type="text" onkeypress='catchtabenter(
Main Topics
Browse All TopicsHi,
I'm having the hardest time with this, I know there are plenty of examples to do this, but I can't seem to get it working.
I have a page that shows a grid of input textboxes. When someone hits tab OR enter in these boxes, I want to fire a function. I'm having trouble finding a cross-browser way to determine what keystroke they pressed. Does someone have a quick function that will catch a keypress in a input box, and depending on if they hit a tab or enter, run some code. I need this to work, at the very least, in IE and Firefox.
Thanks,
--MIchael
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
As I wrote earlier doAction is customizable...
Look at the following example,
<html>
<head>
<title>Enter and Tab </title>
<script language="javascript">
function BrowserType () {
var srchText = navigator.userAgent;
var brwTypes = ("Opera,MSIE,Netscape,Fire
for (var ix=0; ix < brwTypes.length; ix++) {
if (srchText.toString().match
return brwTypes[ix];
}
}
return null;
}
var pK = 0;
var brwType = BrowserType();
function doAction (evt) {
window.status = 'Event Type ' + evt.type + ' key ' + pK + ' Parameter ' + savedParam;
return false;
}
function kH(e) {
evt = (e) ? e : window.event;
var type = evt.type;
pK = e ? e.which : window.event.keyCode;
if (pK == 13) {
doAction (evt);
return false;
}
if (pK == 9 || pK == 0) {
doAction (evt);
return false;
}
}
function DisableEnterAndTab ()
{
if (!brwType) { return; }
if (brwType == 'MSIE') {
document.onkeypress = kH;
document.onkeydown = kH;
}
else if (brwType == 'Firefox') {
document.onkeypress = kH;
}
else if (brwType == 'Netscape') {
document.onkeypress = kH;
if (document.captureEvents) {
document.captureEvents (Event.KEYPRESS);
}
}
else {
alert ('UnSupported Browswer');
}
}
DisableEnterAndTab();
var savedParam = null;
function catchtabenter (evt, params) {
savedParam = params;
kH(evt);
return true;
}
</script>
</head>
<body>
<form name="MyForm" onSubmit="return false;">
<br>Event onkeydown and onkeypress assigned to following text box.
<input type="text" name="text1" onkeydown='return catchtabenter(event, "somedataIneed");' onkeypress='return catchtabenter(event, "somedataIneed");' />
<br><input type="submit">
</form>
</body>
</html>
Thanks, here's what I ended up using, it works great between both browsers
<script language="javascript">
var nextID = null;
function kH(e) {
var code;
if (!e) var e = window.event
if (e.keyCode) code = e.keyCode;
else if (e.which) code = e.which;
if((code==13)||(code==9))
{
document.getElementById(ne
e.cancelBubble = true;
if (e.stopPropagation) e.stopPropagation();
return false;
}else{
return true;
}
}
function catchtabenter (evt, cNextID) {
nextID = cNextID;
return kH(evt);
}
</script>
<input type="text" name="text1" onkeydown='return catchtabenter(event, "somedataIneed");' />
Business Accounts
Answer for Membership
by: pravinasarPosted on 2006-07-11 at 11:42:49ID: 17084180
This is a example of how to detect (and prevent) tab and enter key event on text fields.
fox").spli t(','); (brwTypes[ ix])) {
This is a cross browser solution, try it yourself. You can plug in appropriate action (doAction()...)
<html>
<head>
<title>Disable Enter and Text </title>
<script language="javascript">
function BrowserType () {
var srchText = navigator.userAgent;
var brwTypes = ("Opera,MSIE,Netscape,Fire
for (var ix=0; ix < brwTypes.length; ix++) {
if (srchText.toString().match
return brwTypes[ix];
}
}
return null;
}
var brwType = BrowserType();
function doAction (evt) {
return false;
if (evt) {
evt.returnValue = false;
evt.cancelBubble = true;
}
else {
alert ('Bad Event Object');
}
}
function kH(e) {
evt = (e) ? e : window.event;
var type = evt.type;
var pK = e ? e.which : window.event.keyCode;
if (pK == 9) { pK = 13; evt.keyCode = 13; }
if (pK == 13) {
doAction (evt);
return false;
}
if (pK == 9 || pK == 0) {
doAction (evt);
return false;
}
}
function DisableEnterAndTab ()
{
if (!brwType) { return; }
if (brwType == 'MSIE') {
document.onkeypress = kH;
document.onkeydown = kH;
}
else if (brwType == 'Firefox') {
document.onkeypress = kH;
}
else if (brwType == 'Netscape') {
document.onkeypress = kH;
if (document.captureEvents) {
document.captureEvents (Event.KEYPRESS);
}
}
else {
alert ('UnSupported Browswer');
}
}
DisableEnterAndTab();
</script>
</head>
<body>
<form name="MyForm" onSubmit="alert('No you did not disable them'); return;">
<br>
<input type="text" name="t1" value="">
<br>
<input type="text" name="t2" value="">
<br>
<input name="TheText">
<br>
<input type="submit">
</form>
</body>
</html>