Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Javascript Detect Return ( chr 13)

Posted on 2004-11-01
9
Medium Priority
?
1,158 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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 …
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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…

610 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