Solved

Javascript Detect Return ( chr 13)

Posted on 2004-11-01
1,090 Views
Last Modified: 2012-05-05
simple ( should be )

two fields on a page  
field1  
field2

field1 has an onKeyPress event to trigger a function

bar code scanner scans in   bar code.  
I need to detect the chr(13) at the end of the feed  to trigger move to field2

can anyone help please.
0
Question by:SeanLWilliams
    9 Comments
     
    LVL 49

    Accepted Solution

    by:
    Look at the documentation from http://www.javascriptkit.com/javatutors/javascriptkey7.shtml and decide which browsers you want to support.

    Typically you can use:

    <script type="Text/javascript">
    function captureReturnAsTab(next_field)
    {
      if(next_field && window.event.keycode == 13)
      {
         next_field.focus();
         return false;
      }
      return true;
    }
    </script>
    <input name="field1" type="text" onkeypress="return captureReturnAsTab(this.form.field2);"/>
    <input name="field1" type="text"/>
    0
     
    LVL 32

    Expert Comment

    by:ldbkutty
    This should work in ALL Browsers:

    <html>
    <head>
    <script type="text/javascript">
    function handleEnter (field, event) {
          var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
          if (keyCode == 13) {
                for (var i = 0; i < field.form.elements.length; i++)
                      if (field == field.form.elements[i])
                            break;
                i = (i + 1) % field.form.elements.length;
                field.form.elements[i].focus();
                return false;
          }
          else
                return true;
    }
    </script>
    </head>

    <body>
    <form action="page2.html">
    <input type="text" onkeypress="return handleEnter(this, event)"><br>
    <input type="text" onkeypress="return handleEnter(this, event)"><br>
    <textarea onkeypress="return handleEnter(this, event)"></textarea><br>
    <textarea onkeypress="return handleEnter(this, event)"></textarea><br>
    <input type="submit" value="submit form" onkeypress="return handleEnter(this, event)">
    </form>
    </body>
    </html>

    This is what i use for handling Enter Key in NETSCAPE ...because window.event is not directly supported by Netscape ( not sure, read somewhere)...
    0
     
    LVL 49

    Expert Comment

    by:Roonaan
    var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;

    should be:

    var keyCode = event.keyCode ? event.keyCode : (event.which ? event.which : event.charCode);
    or
    var keyCode = event.which ? event.keyCode ? event.keyCode : event.which : event.charCode;

    -r-

    0
     
    LVL 32

    Expert Comment

    by:ldbkutty
    <html>
    <head>
    <script type="text/javascript">
    function handleEnter (field, event) {
          var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
          if (keyCode == 13) {
                for (var i = 0; i < field.form.elements.length; i++)
                      if (field == field.form.elements[i])
                            break;
                i = (i + 1) % field.form.elements.length;
                field.form.elements[i].focus();
                return false;
          }
          else
                return true;
    }
    </script>
    </head>
    <body>
    <form action="page2.html">
    <input type="text" name="field1" onkeypress="return handleEnter(this, event)"><br>
    <input type="text" name="field2" onkeypress="return handleEnter(this, event)"><br>
    <input type="text" name="field3" onkeypress="return handleEnter(this, event)"><br>
    <input type="submit" value="submit form">
    </form>
    </body>
    </html>

    Just remove the onkeypress event from the text fields where you dont need.
    0
     
    LVL 32

    Expert Comment

    by:ldbkutty
    var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
    and
    var keyCode = event.keyCode ? event.keyCode : (event.which ? event.which : event.charCode);

    both are same, right?
    0
     
    LVL 49

    Expert Comment

    by:Roonaan
    No, they are not.

    In the first case javascript cannot decide actually which ':' of the two splits the first '?' and therefor could turnout as an javascript error on some systems.

    -r-
    0
     
    LVL 2

    Author Comment

    by:SeanLWilliams
    thanks :)


    0
     
    LVL 2

    Author Comment

    by:SeanLWilliams
    it was basically just the event.keycode  statement I needed,  got it working now anyway , thanks
    0
     
    LVL 32

    Expert Comment

    by:ldbkutty
    >> In the first case javascript cannot decide actually which ':' of the two splits the first '?' and therefor could turnout as an javascript error on some systems.
    I dont think so. Can you give me an example ?
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Learn The Basics of Ethical Hacking & Pen Testing

    Computer and network security is one of the fastest growing and most essential industries in technology, meaning companies will pay big bucks for ethical hackers. This is the perfect course to leap into this lucrative career, learning how to use ethical hacking to reveal ...

    This article shows how to read a single webpage's HTML into a string variable, and it also shows how to automate a sequence so you can read and process a list of webpages.  I was tasked with reading and parsing a series of webpages to collect some s…
    Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    856 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now