473,396 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

JavaScript For Automatically moving Between Textboxes

Frinavale
9,735 Expert Mod 8TB
Hi there!

I'm trying to write a simple JavaScript that will move the cursor to the next TextBox after 5 characters (or numbers) have been entered. I'm attempting to get this working in all browsers and realize that I'll likely have to write more than one script depending on which the user is using.

Anyways, everything I've tried...I've only come up with something that "sort of" works in IE (only).
The cursor will be moved to the next TextBox when the count reaches 5 but the 5th character is not printed because it's being replaced by the tab event (keyCode=9).

This will not work for FireFox, nor Opera. I've changed "keyCode" to be "charCode".... Alas, it will not tab to the next TextBox...it just prints a space instead.

So, after attempting this solution I tried to use the focus() method instead.

I have no idea why, but this won't work in any browser.

Does anyone have a suggestions on how to go about solving this problem?

I'm pretty stuck.

Thanks :)

-Frinny
Apr 2 '07 #1
2 4963
mrhoo
428 256MB
Why limit yourself to 5?
This script will advance the focus from any input with a maxLength property when the number of digits or characters equals the maxlength.
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <title>Moving Focus</title>
  4.  
  5. <script type="text/javascript">
  6.  
  7. function after5(e){
  8.     var who= e? e.target: event.srcElement;    
  9.     if(who.value.length< who.maxLength) return true;    
  10.     var group= who.form.elements;
  11.  
  12.     var L= group.length-1, tem;
  13.     for(var i=0; i<L; i++){
  14.         tem= group[i];
  15.         if(tem==who && group[i+1]) return group[++i].focus();
  16.     }
  17. }
  18. onload= function(){
  19.     var A=document.getElementsByTagName('input');
  20.     var L= A.length;    
  21.     for(var i=0; i< L; i++){
  22.         var tem=A[i];
  23.         if(tem.maxLength)tem.onkeyup= after5;    
  24.     }
  25.     A[0].focus();
  26. }
  27. </script>
  28. </head>
  29. <body> 
  30. <h1>Moving focus</h1>
  31. <form action='' onsubmit="return false;">
  32. <p><label>First box: 5 characters <input name="in_1" type="text" size="5" maxLength="5" value=""></label></p>
  33. <p><label>Second box: 5 characters <input name="in_2" type="text" size="5" maxLength="5" value=""></label></p>
  34. <p><label>Third box: 3 characters <input name="in_3" type="text" size="3" maxLength="3" value=""></label></p>
  35. <p><label>Fourth box: 7 characters <input name="in_4" type="text" size="7" maxLength="7" value=""></label></p>
  36. <p><label>Fifth box: 5 characters <input name="in_5" type="text" size="5" maxLength="5" value=""></label></p>
  37. <p><input type="button" value="Submit"></p>
  38. </form>
  39. </body>
  40. </html>
  41.  
Apr 2 '07 #2
Frinavale
9,735 Expert Mod 8TB
Why limit yourself to 5?
This script will advance the focus from any input with a maxLength property when the number of digits or characters equals the maxlength.
...
Mr. Hoo you are Awesomel!

Thank you so much!
I never even thought about using the max length.
I ended up finally getting the focus() to change to the next textbox (had a case sensitive issue with my getElementByID() )...

But then ran into a few problems for when the user's input was something like backspace...and even had to write code to check how many characters were in the fields if the focus had changed.

The max length check gets rid of both these problems completely!
This is such a complete solution to my problem.
I hope you don't mind me using it as is.

Thanks again!
:)
-Frinny
Apr 2 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

53
by: Cardman | last post by:
Greetings, I am trying to solve a problem that has been inflicting my self created Order Forms for a long time, where the problem is that as I cannot reproduce this error myself, then it is...
3
by: Peter Williams | last post by:
Hi All, I want to write some javascript for a html page which does the following. Imagine that the page contains a table with 2 columns and 3 rows, e.g.: +---+---+ | A | B | +---+---+
0
by: sramruttun | last post by:
hi I have 2 textboxes for entry of start and end dates. I allow typing in the textboxes also. I have 2 compareValidator fields as well to validate the 2 textboxes which work fine. Upon text...
4
by: Davisro | last post by:
I would like to have a total box show the totol of four textboxes when anyone of them change. I know I could do this via postback, but would like to do this on client side utilizing javascript....
2
by: Luis Esteban Valencia Muñoz | last post by:
I have a datagrid that displays editable text fields (2 different price fields) and a checkbox in every row. It has a "SaveChanges" button at the bottom, which, when pressed, looks at every...
1
by: Sirisha | last post by:
Hi, I want javascript for displaying multiple textboxes dynamically. ADD-this is button. if i click button first time it will display 1 textbox and if i click 2nd time it will display another...
1
by: =?Utf-8?B?UmljaA==?= | last post by:
In a database search application (vb2005), the user wants to be able to scroll through records using the mousewheel. The data display form contains textboxes for the main data and a datagridview...
2
by: davidson1 | last post by:
Hai friends..for menu to use in my website..i found in one website....pl look below website.... http://www.dynamicdrive.com/dynamicindex1/omnislide/index.htm i downloaded 2 files.... ...
0
by: =?Utf-8?B?QWJoaQ==?= | last post by:
Hi All, i have 3 textboxes for area Code,phone number and Extention and each have MaskedEditExtender associated with it. i want that when the length of first text box reaches it's maxlength ,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.