473,767 Members | 2,152 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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="JavaS cript">
<!-- Original: Cyanide_7 (le*****@hotmai l.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.appN ame.indexOf("Ne tscape")!=-1);
function autoTab(input,l en, 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 && !containsElemen t(filter,keyCod e)) {
input.value = input.value.sli ce(0, len);
input.form[(getIndex(input )+1) % input.form.leng th].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.leng th && index == -1)
if (input.form[i] == input)index = i;
else i++;
return index;
}
return true;
}
// End -->
</script>
Jul 23 '05 #1
3 4982
"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<o pt> }
|
| 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,l en) {
if(input.value. length >= len)
input.form[(getIndex(input )+1)].focus();
}

function getIndex(input) {
var index = -1, i = 0;
while (i < input.form.leng th && 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
2606
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 that follows C++ RAII pattern. Of course Python objects are not statics like in C++, but in C++ the auto_ptr class is used for enforcing this pattern for dynamical
9
2515
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 auto-newline "minor mode" facility. If you could spare a little time, would you answer these questions, please: o Do you program with auto-newline switched on (e.g. do you get NLs inserted automatically after typing a `;' or `{')? o Did you configure...
20
2874
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
2053
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 QueryString. (We'll call the page it goes to the "destination page".) If the user hits BACK on the destination page it should goto the "Auto Jump" page which should put you right back to where you hit the button. In Page_Load of this "Auto Jump"...
5
5044
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 CurrentUICulture to what's specified in the querystring. Since I want to allow UICulture auto detecting, I add UICulture = "auto" to page directive on each page.
5
3277
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 I change 'auto' to 0 for left and right margin values; I have to leave those at 'auto'.. so I would like to know what exactly means 'auto' -- what value it represents exactly (and does it apply for all elements/values you might apply 'auto' to?)
13
4728
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 i type in the details. I have just moved over to mysql server with access as the front end. I have setup the sql tables with the customer number as autonumber. When i go into the form and add a new customer it does not generate the
22
3083
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 like that: int main(char argc, char *argv, char *env) { try { auto Exception mainException(1); mainException.setErrNo(42);
2
3079
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 ("auto") in JavaScript - however instead of getting "auto" value, I get calculated height. In IE and Opera it simply returns "auto". Any ideas how to check in Firefox if element height was set to "auto" ? I'll be grateful for any help.
21
6354
by: JOYCE | last post by:
Look the subject,that's my problem! I hope someone can help me, thanks
0
9571
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9405
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10169
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10013
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9960
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6655
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5424
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3930
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3533
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.