1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How to catch the 'tab' key in a textfield??

Discussion in 'PHP' started by lost, Oct 17, 2005.

  1. #1
    I need to figure when the 'tab' key has been selected inside a textfield.
    The result of pressing the tab would be to create a new row of textfields.

    Anyone, please advise...
     
    lost, Oct 17, 2005 IP
  2. durango

    durango Guest

    Messages:
    83
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Use the onkeydown event:

    <input type="text" name="mytext" onkeydown="if(event.keyCode==9) {makeRow();}">
    Code (markup):
     
    durango, Oct 17, 2005 IP
  3. lost

    lost Peon

    Messages:
    144
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks again Durango, but i'm not sure on the syntax on writing methods in php?? Help please :S
     
    lost, Oct 17, 2005 IP
  4. durango

    durango Guest

    Messages:
    83
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #4
    lost,

    The only way to catch a keypress in the browser is with client-side scripting, such as the onkeydown javascript event I used in the code example. That can be used to fire a javascript method to either create new form elements dynamically using javascript or to submit the form to PHP and redraw the form with added form elements.
     
    durango, Oct 17, 2005 IP
  5. lost

    lost Peon

    Messages:
    144
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Ok, so if i want to create the rows dynamically, i would have to use javascript method.
    Now, do i write this javascript method within the php code?? whats the format? i'm so lost...
     
    lost, Oct 17, 2005 IP
  6. djDeathx

    djDeathx Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    read the data from the textfield, then you have the following options:

    -consider 4 or more spaces a tab
    -read each word (tokenize the input somehow) and check to see whether a word equals to "\t" (if it works)
     
    djDeathx, Oct 17, 2005 IP
  7. mnemtsas

    mnemtsas Super Dud

    Messages:
    497
    Likes Received:
    40
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I had this problem just yesterday. I can tell you now:

    <input type="text" name="mytext" onkeydown="if(event.keyCode==9) {makeRow();}">
    Code (markup):
    wont work, onkeydown won't catch the TAB (ascii 9) character. I found a solution that worked in IE but there doesn't appear to be a solution for mozilla/FF.

    EDIT

    Here's the IE solution, by the way: http://www.experts-exchange.com/Programming/Q_21280185.html
     
    mnemtsas, Oct 18, 2005 IP
  8. durango

    durango Guest

    Messages:
    83
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #8
    mnemtsas,

    The article you posted shows using onkeydown. And I have tested onkeydown in IE, and it works.
     
    durango, Oct 18, 2005 IP
  9. Dread

    Dread Peon

    Messages:
    323
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    0
    #9
    lost: you first need to understand the difference between javascript and php. One is client side and one is server side.

    In php you can catch a tab by looking for \t in double quotes, obviously the method you do this depends on the context of the situation.
     
    Dread, Oct 18, 2005 IP
  10. mnemtsas

    mnemtsas Super Dud

    Messages:
    497
    Likes Received:
    40
    Best Answers:
    0
    Trophy Points:
    0
    #10
    I tested it too, and TAB tabs out of the field when using onkeydown to trap the tab.
     
    mnemtsas, Oct 18, 2005 IP
  11. durango

    durango Guest

    Messages:
    83
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #11
    If you try the following HTML and hit tab in the field, does it give you an alert of 9? It did for me on IE6.02

    <input type="text" name="textbox" onkeydown="alert(event.keyCode);">
    Code (markup):
     
    durango, Oct 18, 2005 IP
  12. mnemtsas

    mnemtsas Super Dud

    Messages:
    497
    Likes Received:
    40
    Best Answers:
    0
    Trophy Points:
    0
    #12
    It'll give you an alert, but if you try any of the usual methods (ie returning 0) to stop tabbing out it won't work. Don't ask me why. Very odd.
     
    mnemtsas, Oct 18, 2005 IP
  13. durango

    durango Guest

    Messages:
    83
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Ok, but I think for his purposes, just recognizing that the tab key was pressed and be able to call javascript based on that will work. He can either submit his form or add form elements dynamically if the event.keyCode equals 9 (tab). Doesn't really matter if the tab then moves on, his javascript code has already executed whatever he needed it to do.
     
    durango, Oct 18, 2005 IP
  14. Arnica

    Arnica Peon

    Messages:
    320
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #14
    You can cancel the tab by using the onkeypress event in Firefox.
    onkeypress="doStuff();return false;"
    Code (markup):
    Presumably this is because the onkeypress event still fires even if you cancel the onkeydown event.

    Mick
     
    Arnica, Oct 18, 2005 IP