SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot
    Join Date
    Sep 2000
    Location
    Birmingham, AL
    Posts
    120
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    javascript pressing enter to tab won't select textfield

    I have a webpage where I have a form set up to tab to the next field when the user presses enter. The problem is that there is existing data in fields sometimes and the cursor instead of highlighting the existing text, places itself in front of the text. Not good.

    This is for a handheld scanner/pda, so the user can't hit tab or click with a mouse.

    So, the ideal situation is for when the user presses enter, that the next field is highlighted so the text can be replaced. Any ideas how I could accomplish this?

    Code for js
    Code:
    <SCRIPT LANGUAGE="JavaScript">
    <!-- Begin
    nextfield = "Product"; // name of first box on page
    netscape = "";
    ver = navigator.appVersion; len = ver.length;
    for(iln = 0; iln < len; iln++) if (ver.charAt(iln) == "(") break;
    netscape = (ver.charAt(iln+1).toUpperCase() != "C");
    
    function keyDown(DnEvents) { // handles keypress
    // determines whether Netscape or Internet Explorer
    k = (netscape) ? DnEvents.which : window.event.keyCode;
    if (k == 13) { // enter key pressed
    if (nextfield == 'begin') return true; // submit, we finished all fields
    else { // we're not done yet, send focus to next box
    eval('document.yourform.' + nextfield + '.focus()');
    return false;
          }
       }
    }
    document.onkeydown = keyDown; // work together to analyze keystrokes
    if (netscape) document.captureEvents(Event.KEYDOWN|Event.KEYUP);
    //  End -->
    </script>
    Code for textfield
    Code:
    <input name="Product" onclick="this.focus();this.select();" onFocus="nextfield ='PO';" 
    type="Text" value="<!--[CBW.Data=Product]-->" size="15">
    Joseph Schell
    AMICO-Online

  2. #2
    SitePoint Zealot
    Join Date
    Sep 2000
    Location
    Birmingham, AL
    Posts
    120
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nevermind, got it to work...

    Code:
    eval('document.yourform.' + nextfield + '.focus()');
    changed .focus() to .select()
    Joseph Schell
    AMICO-Online

  3. #3
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Please...ditch that eval()....
    Code:
    document.yourform[nextfield].select();
    ::: certified wild guess :::


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
  •