Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > How can I capture tab key event and cancel default action of Tab key?

Thread Tools

How can I capture tab key event and cancel default action of Tab key?

Posts: n/a
The default action of tab key is to move focus.
For example,
<textarea id="ta"></textarea>
<textarea id="tb"></textarea>

Pressing Tab key when cursor is in the first textarea results in cursor
in the second textarea.
Is there a cross-browser way to capture the event of pressing Tab key
in a textarea and canceling the focus-moving action?

I tried the following code:

<textarea id="ta"></textarea>
<textarea id="tb"></textarea>
document.getElementById('ta').onkeypress = function(e){
e = e || window.event;
var code = e.keyCode || e.which;
var c = String.fromCharCode(code);
if(c == '\t'){
alert('TAB pressed'); return false;
} else return true;

And I put cursor in the first textarea and pressed Tab.
IE 7 showed no alertbox and focus moved. not ok.
Firefox 2.0 showed alertbox and focus stayed. ok.
Opera 9 showed alertbox and focus moved. not ok.

Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
How to capture and then cancel Tab key event. HopfZ Javascript 0 01-08-2007 10:12 PM
DetailsView - how to capture the click event on Edit and Cancel command? K B ASP .Net 1 03-29-2006 02:15 AM
HELP CANCEL CANCEL CANCEL Carmen Rosario Computer Support 7 04-06-2005 11:04 PM
Replace Tab Key to Return Key (Enter Key) from Web Forms? M P ASP General 1 08-06-2004 08:32 AM