[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Javascript Detect Return ( chr 13)

Posted on 2004-11-01
9
Medium Priority
?
1,184 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
Comment
Question by:SeanLWilliams
  • 4
  • 3
  • 2
9 Comments
 
LVL 49

Accepted Solution

by:
Roonaan earned 2000 total points
ID: 12461812
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
ID: 12461883
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
ID: 12461933
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
LVL 32

Expert Comment

by:ldbkutty
ID: 12461950
<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
ID: 12461969
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
ID: 12462001
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
ID: 12462041
thanks :)


0
 
LVL 2

Author Comment

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

Expert Comment

by:ldbkutty
ID: 12462163
>> 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

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
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…
Suggested Courses

607 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