Javascript Detect Return ( chr 13)

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.
LVL 2
SeanLWilliamsAsked:
Who is Participating?
 
RoonaanCommented:
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
 
ldbkuttyCommented:
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
 
RoonaanCommented:
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
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
ldbkuttyCommented:
<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
 
ldbkuttyCommented:
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
 
RoonaanCommented:
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
 
SeanLWilliamsAuthor Commented:
thanks :)


0
 
SeanLWilliamsAuthor Commented:
it was basically just the event.keycode  statement I needed,  got it working now anyway , thanks
0
 
ldbkuttyCommented:
>> 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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.