473,407 Members | 2,306 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,407 software developers and data experts.

Directing the cursor to a input field

JW
hello everybody
I've got the following problem. I'm writing a form and I'm using javascript
to validate fields (i.e. numeric, not blank). When my scripts detects an
error I want it to put the cursor in the corresponding field.
I've tried the focus option, but that doesn't seem to work.
This is the code I'm using:

if (TestOk=="N")
{
alert('Veld mag niet leeg zijn');
veld.select();
veld.focus();
}

The veld.select() does select the error string, the focus, however doesm't
seem to work.
Anybody got a suggestion?
Thanks anyway
JW
Jul 23 '05 #1
7 6742
> The veld.select() does select the error string, the focus, however doesm't
seem to work.
Try document.Formname.Fieldname.focus();

where 'Formname' is the name of your form and 'Fieldname' is the name of the
field you want to focus on.

If that doesn't work (it should though),what error message do actually get?

Thx
Stu
"JW" <ja*******@hotpop.com> wrote in message
news:%ayBc.4629$%27.144199@zonnet-reader-1... hello everybody
I've got the following problem. I'm writing a form and I'm using javascript to validate fields (i.e. numeric, not blank). When my scripts detects an
error I want it to put the cursor in the corresponding field.
I've tried the focus option, but that doesn't seem to work.
This is the code I'm using:

if (TestOk=="N")
{
alert('Veld mag niet leeg zijn');
veld.select();
veld.focus();
}

The veld.select() does select the error string, the focus, however doesm't
seem to work.
Anybody got a suggestion?
Thanks anyway
JW

Jul 23 '05 #2
JW
I've tried that format, but it didn't give me any results. I don't get any
error message, the cursor just doesn't go where I want it to.
JW

The veld.select() does select the error string, the focus, however doesm't seem to work.
Try document.Formname.Fieldname.focus();

If that doesn't work (it should though),what error message do actually

get?
Thx
Stu

Jul 23 '05 #3
Can you supply your full code? (JS and forms html), what browsers are you
using too?

Stu

"JW" <ja*******@hotpop.com> wrote in message
news:pLBBc.4645$%27.145901@zonnet-reader-1...
I've tried that format, but it didn't give me any results. I don't get any error message, the cursor just doesn't go where I want it to.
JW

The veld.select() does select the error string, the focus, however doesm't seem to work.


Try document.Formname.Fieldname.focus();

If that doesn't work (it should though),what error message do actually

get?

Thx
Stu


Jul 23 '05 #4
JW
The script I'm using is like the following one. It has to work in IE. Other
browsers are not important for now, because it's meant as a field check for
a business application.
When you use the buttons to set focus to the appropriate field the function
used to setfocus works perfectly.
When the NotEmpty function is triggered by leaving the input field the focus
stays on the next selected field.
When you want to see this code working visit :
http://home.zonnet.nl/janwouter/script/focus.html

<html>
<head>
<script lang=javascript>
function NotEmpty(veld)
{
TestOk="N";
invoer=veld.value.toString();
for(var i=0;i<invoer.length;i++)
{
var teken =invoer.charAt(i);
if (teken != ' ')
{
TestOk="J";
i=invoer.length;
}
}
if (TestOk=="N")
{
/* alert('Veld mag niet leeg zijn');*/
veld.value="Field may not be empty !!";
focusonfield(veld);
}
} /*einde functie NotEmpty*/

function focusonfield(field)
{

if (field.name=="inp1")
{document.form1.inp1.focus()}

if (field.name=="inp2")
{document.form1.inp2.focus()}

if (field.name=="inp3")
{document.form1.inp3.focus()}

}
</script>
</head>
<body>
<form name ="form1">
<input name="inp1" value="veld1" onchange="NotEmpty(this)";/>
<input name="inp2" value="veld2" onchange="NotEmpty(this)";/>
<input name="inp3" value="veld3" onchange="NotEmpty(this)";/>
<br/>
<button name="but1" onclick="focusonfield(form1.inp1)">veld1</button>
<button name="but2" onclick="focusonfield(form1.inp2)">veld2</button>
<button name="but3" onclick="focusonfield(form1.inp3)">veld3</button>
</form>
</body>

"Stuart Palmer" <tr**********@youcant.com> schreef in bericht
news:2j*************@uni-berlin.de...
Can you supply your full code? (JS and forms html), what browsers are you
using too?

Stu

Jul 23 '05 #5
Lee
JW said:

The script I'm using is like the following one. It has to work in IE. Other
browsers are not important for now, because it's meant as a field check for
a business application.
When you use the buttons to set focus to the appropriate field the function
used to setfocus works perfectly.
When the NotEmpty function is triggered by leaving the input field the focus
stays on the next selected field.


The problem seems to be that moving focus to the next field occurs
after the onChange event has completed and it can't be cancelled.

A hack to work around this is to let the focus move to the next
field, but then bring it back to where you want it by scheduling
focus to return after a short delay:

function focusonfield(field)
{
var fieldRef="document.forms['" + field.form.name
+"'].elements['" + field.name+"']";
setTimeout(fieldRef+".select();"+fieldRef+".focus( )",10);
}

Jul 23 '05 #6
JW
Makes sense. I'm gonna try it today and let you know the results.
Thanks
JW
The problem seems to be that moving focus to the next field occurs
after the onChange event has completed and it can't be cancelled.

Jul 23 '05 #7
JW
It worked great! Thanks for your help.
JW
Makes sense. I'm gonna try it today and let you know the results.
Thanks
JW
The problem seems to be that moving focus to the next field occurs
after the onChange event has completed and it can't be cancelled.


Jul 23 '05 #8

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

Similar topics

6
by: Gonzosez | last post by:
When a page is opened is there a way to set focus of the cursor to a named input. I want the cursor to be in the input box when the page is opened. Thanks
9
by: Gary Holt | last post by:
We have a problem demonstrated by the following HTML document. To see the problem type HV in the first field then use the mouse cursor to click into the Staff code input box. You will find that...
5
by: Rune Runnestř | last post by:
How do I focus the cursor in the input field 'numberField' when accessing this jsp-file (or html-file) ? <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head>...
7
by: William Gill | last post by:
I have been trying to pass parameters as indicated in the api. when I use: sql= 'select * from %s where cusid = %s ' % name,recID) Cursor.execute(sql) it works fine, but when I try : sql=...
4
by: Filips Benoit | last post by:
Dear All, How to clearly show the active (cursor) field. A very large form having about 100 inputfields. After pausing input the user must see a once where the cursor is! Thanks, Filip
1
by: AA Arens | last post by:
(1) When I want the data in the datafield starts with the first character forcedly uppercased, I use the input mask >L<, followed by ?????. When entering the data, the field is marked with...
11
idsanjeev
by: idsanjeev | last post by:
how can set cursor location everytime when page is open cursor goes to auto in input field and chake when click on submit lenth of input field
3
by: Klaus Brune | last post by:
Hello all, When one tabs through a form (specifically, in Firefox), all the text in a field is automatically selected. What I'm looking for is a way to put a function (in onFocus perhaps)...
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: 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:
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
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
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.