473,396 Members | 1,871 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.

Auto-advance in html form input boxes


Somebody asked me if it would be possible to add auto-advance to a web
form where there are a lot of repetitive 5 character fields. I took a
look around the web and found a script, which appears to work in the
couple of browsers I tried it in. However, when I look at the script it
appears to have the wrong number of brackets. Then when I changed the
script to the way I thought it should be, it still worked!

I'm totally rusty on javascript. I think I remember that you're allowed
to omit brackets when you only have 1 line following the if (or while,
or whatever).

A couple of questions:

Are the brackets in the original script below wrong or am I missing
something?

Can anyone recommend a different auto-advance script?

Or, is auto-advancing a bad idea all around from a
usability/accessibility point of view?

thx,

--wmc.


<SCRIPT LANGUAGE="JavaScript">
<!-- Original: Cyanide_7 (le*****@hotmail.com) -->
<!-- Web Site: http://members.xoom.com/cyanide_7 -->

<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->

<!-- Begin
var isNN = (navigator.appName.indexOf("Netscape")!=-1);
function autoTab(input,len, e) {
var keyCode = (isNN) ? e.which : e.keyCode;
var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
if(input.value.length >= len && !containsElement(filter,keyCode)) {
input.value = input.value.slice(0, len);
input.form[(getIndex(input)+1) % input.form.length].focus();
}
function containsElement(arr, ele) {
var found = false, index = 0;
while(!found && index < arr.length)
if(arr[index] == ele)
found = true;
else
index++;
return found;
}
function getIndex(input) {
var index = -1, i = 0, found = false;
while (i < input.form.length && index == -1)
if (input.form[i] == input)index = i;
else i++;
return index;
}
return true;
}
// End -->
</script>
Jul 23 '05 #1
3 4965
"williamc" <te************@williamc.com> wrote:
I'm totally rusty on javascript. I think I remember that you're allowed
to omit brackets when you only have 1 line following the if (or while,
or whatever).
This is correct. As with many languages, you can do this:

if (condition)
statement;

or with braces,

if (condition) {
statement;
}

Holy wars erupt when discussing code formatting, so be careful :)
I think that braces should _never_ be left off. It's always clearer with
braces included, and when you later go to add another statement that should
be part of the if block, you don't create a bug if you forget to add the
braces.
Even for simple conditions, I prefer to do

if (condition) { statement; }
Are the brackets in the original script below wrong or am I missing
something?
They are fine, but the formatting is confusing. Leaving them off serves no
purpose other than to confuse!
Or, is auto-advancing a bad idea all around from a
usability/accessibility point of view?


That's up for debate, but I think they're annoying. They break the
convention that users are used to, unless this is a form that will be
repeatedly used by the same people on an intranet or something.

When I encounter an auto-advance script, I always end up hitting tab anyway,
and advancing two, then typing in the wrong box, then deleting, then
shift-tabbing back to the box I was in. Unless there is some clear visual
marker telling the user that their cursor will be automatically advanced, or
if the user is used to it and knows what to expect, I think it's a bad idea.

--
Matt Kruse
Javascript Toolbox: http://www.mattkruse.com/javascript/

Jul 23 '05 #2
williamc wrote:
<snip>
I'm totally rusty on javascript. I think I remember that you're
allowed to omit brackets when you only have 1 line following the if
(or while, or whatever).
Strictly, the brackets are not part of the - if - else - construct at
all, and lines don't come into it either as javascript tolerates a
liberal distribution of white space characters, including line breaks,
within statements. The - if - productions are:-

| IfStatement :
| if ( Expression ) Statement else Statement
| if ( Expression ) Statement

- so - if - (and - else) are flowed by _exactly_one_ statement (which
may be broken across as many lines as desired).

Where the brackets enter the picture is with the Block statement:-

| Block :
| { StatementList<opt> }
|
| StatementList :
| Statement
| StatementList Statement

A block statement is a single statement, but may contain as many other
statements as is desired.

The same is true for - for - , - while -, etc.
A couple of questions:

Are the brackets in the original script below wrong or
am I missing something?
The syntax looks valid. Confused a bit by two inner functions and the
lack of the use of the Block statement to make the grouping of
statements related to - if - and loop statements clear. Correctness, in
this context, goes into the area of coding style, I wouldn't have
omitted containing block statements and JSLINT would not pass this
script as it is.
Can anyone recommend a different auto-advance script?
No, but I don't think this one should be used. It includes browser
detecting, it only seems interested in two browser types, the use of
inner functions makes it slower than it needs to be and the extensive
use of HTML style comments in a script context suggests that its author
was not particularly confident in their javascript authoring when it was
created.
Or, is auto-advancing a bad idea all around from a
usability/accessibility point of view?

<snip>

People who use their keyboard for navigation will enter the required
information in the first field and hit the tab key instinctively. They
will not be happy to find themselves typing into some field after the
next. If the point is to allow the user to enter all of the information
in one sequence then the use of more than one field becomes
questionable. Presumably both client-side and server-side validation
could apply whatever rules would be used to auto-advance to the next
field in splitting the information in a single field up for validation
and storage.

Richard.
Jul 23 '05 #3


Richard Cornford wrote:

No, but I don't think this one should be used. It includes browser
detecting, it only seems interested in two browser types, the use of
inner functions makes it slower than it needs to be and the extensive
use of HTML style comments in a script context suggests that its author
was not particularly confident in their javascript authoring when it was
created.

I'm not even sure why the original author was detecting NN and passing a
ref to the event to autoTab() since if only auto-advancing was required,
something simple like this seems to work as well as his original
script...

function autoTab(input,len) {
if(input.value.length >= len)
input.form[(getIndex(input)+1)].focus();
}

function getIndex(input) {
var index = -1, i = 0;
while (i < input.form.length && index == -1)
if (input.form[i] == input)index = i;
else i++;
return index;
}

re: the usability question. I hear ya. My original comment was what's
wrong with the tab key?

Although at most people's typing speeds the cursor jumps fast enough so
they might avoid the unwanted hitting of the tab key, the script also
breaks shift tabbing back to a full input box.

thx for the reply...

--williamc
Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Manlio Perillo | last post by:
Hi. This post follows "does python have useless destructors". I'm not an expert, so I hope what I will write is meaningfull and clear. Actually in Python there is no possibility to write code...
9
by: Alan Mackenzie | last post by:
To all those who use (X)Emacs's CC Mode to edit C, C++, Java, Objective-C, Pike, AWK or IDL: To help direct the development of CC Mode, it would be useful to find out how people use the...
20
by: Vijay Kumar R. Zanvar | last post by:
Hello, Unlike register, auto keyword can not be used to declare formal parameter(s). Is there any specific reason for this? Kind regards, Vijay Kumar R. Zanvar
2
by: VB Programmer | last post by:
I created a page which I am using to prevent the user from hitting the BACK button. I'll call it my "Auto Jump" page. When it is called it basically auto-redirects to a page specified in the...
5
by: Samuel | last post by:
Hi, I am running into a problem of mixing UICulture = auto and allowing users to select culture using a dropdown list. I am detecting a querystring, "setlang", and when found, setting the...
5
by: maya | last post by:
at work they decided to center divs thus: body {text-align:center} #content {width: 612px; text-align:left; margin: 0 auto 0 auto; } this works fine in IE & FF, EXCEPT in FF it doesn't work if...
13
by: S.Dickson | last post by:
I had an access database that i use as an ordering system. I have a form for entering customer details. When i add a new customer on the form the customer number is an auto number that appears when...
22
by: nospam_news | last post by:
I currently get asked about my usage of "auto". What is it for? The keyword is clearly superflous here. In contrast to the huge majority of C/C++ developers I write definitions very explicitly...
2
by: Piotr K | last post by:
Hi, I've encountered a strange problem with Firefox which I don't have any idea how to resolve. To the point: I've <divelement with a style "height: auto" and I want to retrieve this value...
21
by: JOYCE | last post by:
Look the subject,that's my problem! I hope someone can help me, thanks
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.