CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2005
    Posts
    172

    Input Box and enter

    Hello ,

    I have a bunch of HTML input text boxes(spread over 2 pages) and I need to do the following two things:

    1) On the first page I have an input box and when the user enters the data and presses the
    ENTER Key I need to go to the second page/url

    2) The second thing I need to do is the second page which has many input boxes I need to be
    able to enter data in a box and when the user presses the ENTER key the focus moves to the
    next input box.

    How can I accomplish that with JavaScript

    thanks very much in advance for any suggestions

    Susan

  2. #2
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Input Box and enter

    Why ask a JavaScript question in a Java forum? They are completely different languages.

    The greatest obstacle to discovery is not ignorance, but the illusion of knowledge...
    D. Boorstin
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  3. #3
    Join Date
    Feb 2008
    Posts
    966

    Re: Input Box and enter

    While I agree with dlorde that you should post this in the Client Side Scripting section, this is a relatively easy question.

    When you hit the enter key while in a text box the "onChange" event is triggered. You can then either call a javascript function that will change the focus OR you can assign it directly in the onchange as such:

    <input type="text" name="text1" onChange="this.form.text2.focus()">
    <input type="text" name="text2" onChange="this.form.text1.focus()">

    This silly example will switch from text 1 to text 2 back to text 1 every time you hit enter. If you haven't entered anything into the field (not a required field) use the onBlur instead of onChange (or both).

    As far as your other question you can do the same thing, except call the submit button:

    <input type="text" name="text1" onChange="this.form.submit()">

  4. #4
    Join Date
    Apr 2005
    Posts
    172

    Re: Input Box and enter

    Thanks very much for the replies. I am sorry - I posted this last night and I must have misread it.
    I tried the things u suggested and it works fine except that in addition to the text box I also have a Submit button so hitting Enter calls the javascript to redirect the page and also executes the Submit.

    What should I do to avoid that?

    Thanks

    Susan

  5. #5
    Join Date
    Feb 2008
    Posts
    966

    Re: Input Box and enter

    I think you can use something like:

    Code:
    <script language="javascript" type="text/JavaScript"">
    
    document.onkeypress = keyPress;
    var count = 1;
    function keyPress() {
      if (event.keyCode == 13 && event.srcElement.type != 'textarea' && event.srcElement.type != 'submit')
         document.onkeydown false;
      document.onkeydown true;
    }
    </script>
    This should work for IE anyway.

    You could also try to completely disable the enter key from ever submitting the form by:

    <input type="submit" value="submit" name="saveButton" ONKEYDOWN="if (event.keyCode == 13){return false;}">

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured