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

Discussion in 'Javascript' started by HopfZ, Jan 8, 2007.

  1. HopfZ

    HopfZ Guest

    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>
    <script>
    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;
    }
    </script>

    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.
     
    HopfZ, Jan 8, 2007
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sck10

    Capture FormView Cancel Event

    sck10, Mar 2, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    8,886
    Steven Cheng[MSFT]
    Mar 3, 2006
  2. K B
    Replies:
    1
    Views:
    9,130
    Christopher Reed
    Mar 29, 2006
  3. M P
    Replies:
    1
    Views:
    502
  4. Diego
    Replies:
    4
    Views:
    174
    Randy Webb
    Apr 29, 2006
  5. HopfZ
    Replies:
    0
    Views:
    107
    HopfZ
    Jan 8, 2007
Loading...

Share This Page