SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    England
    Posts
    698
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Enter Acts As Tab

    I've had a request today that I make the enter key act as a tab key on a works web-app.

    "No problem" I thought and stuck a bit of Javascript on each of the inputs that I want to have this behaviour that looks like this:

    onKeyDown="if(event.keyCode==13) event.keyCode=9;"

    Works brilliant in IE, but it doesn't work in FireFox2. Any idea why? Or how to work around it?

    Many thanks

  2. #2
    SitePoint Wizard holmescreek's Avatar
    Join Date
    Mar 2001
    Location
    Northwest Florida
    Posts
    1,707
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think FF uses event.which and event.CharCode

    Try passing the event to a function and then determine of event.keyCode is set, if so set event.keyCode=9

    otherwise

    if event.CharCode==13 set event.CharCode=13

    ----

    This MAY work, you'll just have to try it :

    onKeyDown="if(event.keyCode==13 || event.CharCode==13) { event.keyCode=9; event.CharCode=9; }"
    intragenesis, llc professional web & graphic design

  3. #3
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    England
    Posts
    698
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Gutted! Just tried that solution and it works in IE but no FF. I get the impression that FF may not allow you to intercept and change key presses, though it will record the key press quite happily. Grrr!

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,681
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Firefox and most other browsers don't use the event global variable.


    The key event is usually determined in the following manner

    Code javascript:
    var code = e.which || e.keyCode;

    But in this case we're wanting to do further things with it, that may depend on which type it is, so a branching structure is more appropriate here.

    A function processes the key event in this manner

    Code javascript:
    function captureKeys(e) {
        e = e || window.event;
        if (e.which) {
            // do stuff with e.which
        } else if (e.keyCode) {
           // do stuff with e.keyCode
        }
        ...
    }
    You can find out more about events at http://www.quirksmode.org/js/introevents.html
    You can find out more about detecting keystrokes at http://www.quirksmode.org/js/keys.html
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •