473,385 Members | 1,958 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,385 software developers and data experts.

Capturing <Enter> and moving focus to next tabindex

Hello,

I have been playing with various Googled solutions for capturing the <Enter>
key to suppress form submission.

My first question is whether anyone has a script that works in all common
browsers? The script bellow is IE only. It fails FF 1.0 and NN 7.

Also, if I can trap the keypress I would like to use it to tab to the next
tabindex.

Is that possible? Ca I grab current focused tabindex?

TIA,

jg

__CODE__

<script language=javascript>
<!--

var bIsEnterKey = false;

function checkKeyPress(){

return !bIsEnterKey;

}

function setKeyPress(){
if(event && event.which){ //if which property of event object is
supported (NN4)
characterCode = event.which //character code is contained in NN4's which
property
}
else{
characterCode = event.keyCode;//character code is contained in IE's
keyCode property
}

bIsEnterKey = (characterCode.toString() == '13');

window.setTimeout("bIsEnterKey=false;",1000);

}
//-->

</script>

<form name="form1" method="post" action="xxx.html" onsubmit="return
checkKeyPress();" onkeypress="setKeyPress()">
<input type="text" name="textfield">
</form>
Jul 23 '05 #1
7 20349
Hello Jerry,
capturing the <Enter> key to suppress form submission


When you hit <Enter> on a form that has focus, that triggers a submit
as you have said. Therefore, all you really need is the onsubmit event
handler and just return false. There is no need to check if the
<Enter> key was actually pressed. This will suppress form submission.

Jul 23 '05 #2

Simple:

<form onsubmit="return false">
..............
<input type="button" value="submit now" onclick="this.form.submit()">
</form>

onsubmit="return false" suppresses all enter-press and submit-buttons
submissions, leaving the form alone unsubmitted, then you use onclick to
have it submit.

Danny
On Mon, 11 Jul 2005 12:04:02 -0700, jerrygarciuh
<de*****@no.spam.nolaflash.com> wrote:

--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
Jul 23 '05 #3
Thanks for the reply,

Thing is that approach prevents form submission no matter what.

I want to submit the form only when button is clicked. One problem I am
running into is that onClick events for a submit button are fired when user
hits <Enter> while in a textfield. Hence I can't just set a
document.variable when button is clicked or call a special sub when clicked.

Any thoughts?

jg

"web.dev" <we********@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
Hello Jerry,
capturing the <Enter> key to suppress form submission


When you hit <Enter> on a form that has focus, that triggers a submit
as you have said. Therefore, all you really need is the onsubmit event
handler and just return false. There is no need to check if the
<Enter> key was actually pressed. This will suppress form submission.

Jul 23 '05 #4
jerrygarciuh wrote:

Hi Jerry,
I have been playing with various Googled solutions for capturing the <Enter>
key to suppress form submission.
You certainly have your reasons to do so, but you're taking a dangerous
way IMHO: users are generally aware of their browsers' behavior, and
altering it in some way may always confuse them rather than enhance
their experience - changing the behavior of the ENTER key would likely
lead to such confusion.
My first question is whether anyone has a script that works in all common
browsers? The script bellow is IE only. It fails FF 1.0 and NN 7.


You won't have this in common to all browsers, since (1) the ENTER key
behavior isn't standard (which means that not all browsers behave this
way) and (2) old browsers used to exhibit the behavior on different key
events (keyup, keydown and keypress) which would require the code to
take into account some sort of key events flow (a nightmare, I've done
this before and it was ugly at best).

However, if targeting recent browsers is acceptable to you, then check
the following simple version (tested IE6, Mozilla 1.7 and Opera 8 and
Windows) - given the general issue it's unlikely to work in all recent
browsers, though - please test accordingly.
---
<form action="foo">
<input type="text" tabindex="1">
<input type="text" tabindex="2">
<input type="text" tabindex="3">
<input type="text" tabindex="4">
<input type="submit">
</form>

<script type="text/javascript">
function enter(form){
var fields=form.elements, a=[];

// add ENTER listeners
for(var ii=fields.length; ii--;) {
if(fields[ii].type=="text") {
_e(fields[ii], "onkeypress", enterListener);
a[a.length]=fields[ii];
}
}

// init the tabIndex behavior
a.sort(
function(a,b){
return a.tabIndex > b.tabIndex ? 1 : -1;
}
);

for(var j=0; j<a.length; j++){
a[j].next = a[j+1]||a[0];
}

// add the submission manager
_e(
form,
"onsubmit",
function(evt){
if(form.hasEntered) {
form.hasEntered=false;
return false;
}
return true;
}
);

// ENTER listener
function enterListener(evt){
evt=evt||window.event;
var el=this;
if((evt.keyCode||evt.which)==13) {
form.hasEntered=true;
setTimeout(
function(){ el.next.focus(); },
1
);
}
}
}

// event manager
function _e(obj, evt, func){
if(obj[evt]) {
obj[evt]=(function(h){
return function(evt){
return func.call(this, evt) && h.call(this, evt);
}
})(obj[evt]);
} else {
obj[evt]=func;
}
}

// --- test --- :-)
enter(document.forms[0]);
</script>
---
Regards,
Yep.
Jul 23 '05 #5
Lee
Yann-Erwan Perio said:

jerrygarciuh wrote:

Hi Jerry,
I have been playing with various Googled solutions for capturing the <Enter>
key to suppress form submission.


You certainly have your reasons to do so, but you're taking a dangerous
way IMHO: users are generally aware of their browsers' behavior, and
altering it in some way may always confuse them rather than enhance
their experience - changing the behavior of the ENTER key would likely
lead to such confusion.


I'd like to underscore that point. Changing web forms so that
they behave the way that you, or even your target audience, are
used to seeing forms behave, rather than the default way, is
almost always a bad idea. It's better for everybody to bite the
bullet and learn to use the default interface.

Jul 23 '05 #6
Yep,

Thank you so much! Your solution will save me much headbanging attempting
to obey my client who will not listen to me.

You are very kind to have taken so much time on my behalf!

jg

"Yann-Erwan Perio" <ye*@invalid.com> wrote in message
news:42**********************@news.free.fr...
jerrygarciuh wrote:

Hi Jerry,
I have been playing with various Googled solutions for capturing the
<Enter> key to suppress form submission.


You certainly have your reasons to do so, but you're taking a dangerous
way IMHO: users are generally aware of their browsers' behavior, and
altering it in some way may always confuse them rather than enhance their
experience - changing the behavior of the ENTER key would likely lead to
such confusion.
My first question is whether anyone has a script that works in all common
browsers? The script bellow is IE only. It fails FF 1.0 and NN 7.


You won't have this in common to all browsers, since (1) the ENTER key
behavior isn't standard (which means that not all browsers behave this
way) and (2) old browsers used to exhibit the behavior on different key
events (keyup, keydown and keypress) which would require the code to take
into account some sort of key events flow (a nightmare, I've done this
before and it was ugly at best).

However, if targeting recent browsers is acceptable to you, then check the
following simple version (tested IE6, Mozilla 1.7 and Opera 8 and
Windows) - given the general issue it's unlikely to work in all recent
browsers, though - please test accordingly.
---
<form action="foo">
<input type="text" tabindex="1">
<input type="text" tabindex="2">
<input type="text" tabindex="3">
<input type="text" tabindex="4">
<input type="submit">
</form>

<script type="text/javascript">
function enter(form){
var fields=form.elements, a=[];

// add ENTER listeners
for(var ii=fields.length; ii--;) {
if(fields[ii].type=="text") {
_e(fields[ii], "onkeypress", enterListener);
a[a.length]=fields[ii];
}
}

// init the tabIndex behavior
a.sort(
function(a,b){
return a.tabIndex > b.tabIndex ? 1 : -1;
}
);

for(var j=0; j<a.length; j++){
a[j].next = a[j+1]||a[0];
}

// add the submission manager
_e(
form,
"onsubmit",
function(evt){
if(form.hasEntered) {
form.hasEntered=false;
return false;
}
return true;
}
);

// ENTER listener
function enterListener(evt){
evt=evt||window.event;
var el=this;
if((evt.keyCode||evt.which)==13) {
form.hasEntered=true;
setTimeout(
function(){ el.next.focus(); },
1
);
}
}
}

// event manager
function _e(obj, evt, func){
if(obj[evt]) {
obj[evt]=(function(h){
return function(evt){
return func.call(this, evt) && h.call(this, evt);
}
})(obj[evt]);
} else {
obj[evt]=func;
}
}

// --- test --- :-)
enter(document.forms[0]);
</script>
---
Regards,
Yep.

Jul 23 '05 #7
Lee wrote:
I'd like to underscore that point. Changing web forms so that
they behave the way that you, or even your target audience, are
used to seeing forms behave, rather than the default way, is
almost always a bad idea. It's better for everybody to bite the
bullet and learn to use the default interface.


Except in cases where you're building a web interface to something like an
old mainframe application. You may have several thousand users who have
years of experience hitting the enter key to go between fields. Adding
simple scripts to retain this user-expected functionality may improve
efficiency and accuracy, and make users happier.

In an internet context, tabbing on <enter> or auto-tabbing for things like
phone numbers is always a bad idea, IMO.

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Jul 23 '05 #8

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

Similar topics

11
by: Denis Hierstein | last post by:
I need a function, witch make a break in a for-loop and wait for the <enter>-key ... when I use Pascal I just use the Read; or the ReadLn;-function, then the loop stop as long as the user push the...
4
by: bh | last post by:
So, I am debating with someone about this. He believe that you can set up so when the user presses Enter it acts just like Tab, so it follows the tab order between text boxes in .NET just like you...
7
by: Susan Bricker | last post by:
I know that I saw some information concerning the <shift>+<enter> combination use to bypass launching an Access mdb application and enter the Access design workspace. Would someone please direct...
6
by: tor | last post by:
Hello How can I use an other key then TAB to move from one textBox to another?? Torfinn
0
by: VMI | last post by:
If I'm in a multi-line textbox and I'm writing a postal address (ie. write ist line and press <Enter>, write 2nd line and press <Enter>, etc...) how can I make sure that the Enter key will always...
2
by: JP | last post by:
Hi, It's a login screen, so users enter their ID/Password and hit <ENTER> instead of clicking on Login button. But when they hit <ENTER> an Image Button gets clicked. I want to make my Login...
5
by: DotNetGruven | last post by:
Hi, I have a web form which has: - Login area with - email textbox - password textbox - <enter> button to log in - search area with - string to search for textbox
2
by: Rocio | last post by:
I have a aspx page, with 2 controls (ascx). Control1 contains a SEARCH button, and textbox to enter the string to search for. Control 2 contains a LOGIN button, and 2 text boxes to enter the...
1
by: almurph | last post by:
Hi everyone, I'm a newbie to vb.net. I have written some code to detect when a user presses the down-arrow, up-arrow and enter button. Essentially the user can arrow down or up through a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
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
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,...

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.