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

Set focus after showing an alert?

I have the following function in the onchange event of several text
boxes. If the user enters an invalid number the alert displays. I
would like to return the user to that same field after he closes the
alert but setting the focus as I'm doing here doesn't do it (instead,
the focus goes to the next field).

Any suggestions as to hwo I can make this work?

Thanks.

function checkIt(fldID,maxValue,btnID){
if (document.getElementById(fldID).value > maxValue)
{alert('Invalid Lane Number');
document.getElementById(btnID).disabled=true;
document.getElementById(fldID).focus;}
else
document.getElementById(btnID).disabled=false;
}
Jul 23 '05 #1
5 19261
On Fri, 03 Sep 2004 09:36:57 -0700, Martin <ma**********@comcast.net>
wrote:
I have the following function in the onchange event of several text
boxes. If the user enters an invalid number the alert displays. I would
like to return the user to that same field after he closes the alert but
setting the focus as I'm doing here doesn't do it (instead, the focus
goes to the next field).

Any suggestions as to hwo I can make this work?

Thanks.

function checkIt(fldID,maxValue,btnID){
If this is called from an intrinsic event, it would be much easier, and
more reliable, to follow the pattern below:

<input type="..." ... onchange="checkIt(this,<num>,<id>)">

function checkIt(field, maxVal, btnID) {
var btn = field.form.elements[btnID];
// ...
}

If you do insist on using document.getElementById, at least save the
resulting references instead of calling the method repeatedly.
if (document.getElementById(fldID).value > maxValue)
Assuming that the element represented by fldID should contain a number
(the comparison wouldn't make much sense, otherwise), it would be best to
check that the value actually *is* a number, otherwise you might get
strange results.
{alert('Invalid Lane Number');
document.getElementById(btnID).disabled=true;
document.getElementById(fldID).focus;}
You actually need to call the method!
else
document.getElementById(btnID).disabled=false;
I hope that the button isn't disabled by default. It'll be quite a problem
for users without Javascript.
}


Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #2
Martin wrote:
I have the following function in the onchange event of several text
boxes. If the user enters an invalid number the alert displays. I
would like to return the user to that same field after he closes the
alert but setting the focus as I'm doing here doesn't do it (instead,
the focus goes to the next field).

Any suggestions as to hwo I can make this work?

Thanks.

function checkIt(fldID,maxValue,btnID){
if (document.getElementById(fldID).value > maxValue)
{alert('Invalid Lane Number');
document.getElementById(btnID).disabled=true;
document.getElementById(fldID).focus;}
focus(), not focus.
else
document.getElementById(btnID).disabled=false;
}

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq
Jul 23 '05 #3
On Fri, 03 Sep 2004 17:00:21 GMT, "Michael Winter"
<M.******@blueyonder.co.invalid> wrote:

If you do insist on using document.getElementById, at least save the
resulting references instead of calling the method repeatedly.
OK, I changed that.
You actually need to call the method! I thought I was doing that. If you're referring to the missing
parentheses (as Randy pointed out) then I added them. But it still
doesn't set the focus to the original field.
I hope that the button isn't disabled by default. It'll be quite a problem
for users without Javascript.


Yes the button is disabled by default. The button enables the user to
continue with the process after verifying that the changed value is
valid - I don't want them to continue if that's not the case.

As far as "users without Javascript enabled" is concerned, there won't
be any such thing. This is not what one would call a "normal" web site
that just anybody can use. It's the user interface to an industrial
process. It will be used by a limited number of people on an intranet.
By definition they must have Javascript enabled. And, a number of the
pages are useable only in IE (because I'm using a lot of VML
graphics). They have to log in with a userid and password to even view
any of the pages. There are some other restrictions, too.

Back to my original question. Any thoughts as to how I can get the
focus to go back to the original field?

Thanks.
Jul 23 '05 #4
Any suggestions as to how I can make this work?

Thanks.

function checkIt(fldID,maxValue,btnID){
if (document.getElementById(fldID).value > maxValue)
{alert('Invalid Lane Number');
document.getElementById(btnID).disabled=true;
document.getElementById(fldID).focus;}
else
document.getElementById(btnID).disabled=false;
}


After clearing up the syntax error as noted by Michael & Randy, this
function still did not work - or so I thought. Some further messing
around with it revealed that apparently the focus IS being set to the
input field (fldID) but then (I'm guessing) the TabKey keystroke
executes and moves the cursor to the next field!

I found that, if I mouse away from the field then the cursor will go
back to the field and stay there. But keying away (with either the tab
or Enter keys) results in the focus ending up somewhere else. (in
fact, hitting <Enter> clicks an enabled button on the form AFTER
closing the alert box!)

I also found that, if I call this function from the onblur event
(instead of onchange), it works perfectly. No matter which key is used
the focus goes to the specified field and stays there. Apparently,
onchange and onblur occur at different points relative to the
keystroke itself.

Since I don't want to use onblur, my question now becomes: how can I
(in the function script) cancel the keystroke that occurred. (I'm
coming from a Visual basic perspective here - in VB cancelling a
keystroke is trivial). Is this possible in Javascript?

Thanks.

Jul 23 '05 #5
Martin wrote:
Any suggestions as to how I can make this work?

Thanks.

function checkIt(fldID,maxValue,btnID){
if (document.getElementById(fldID).value > maxValue)
{alert('Invalid Lane Number');
document.getElementById(btnID).disabled=true;
document.getElementById(fldID).focus;}
else
document.getElementById(btnID).disabled=false;
}

After clearing up the syntax error as noted by Michael & Randy, this
function still did not work - or so I thought. Some further messing
around with it revealed that apparently the focus IS being set to the
input field (fldID) but then (I'm guessing) the TabKey keystroke
executes and moves the cursor to the next field!

I found that, if I mouse away from the field then the cursor will go
back to the field and stay there. But keying away (with either the tab
or Enter keys) results in the focus ending up somewhere else. (in
fact, hitting <Enter> clicks an enabled button on the form AFTER
closing the alert box!)

I also found that, if I call this function from the onblur event
(instead of onchange), it works perfectly. No matter which key is used
the focus goes to the specified field and stays there. Apparently,
onchange and onblur occur at different points relative to the
keystroke itself.

Since I don't want to use onblur, my question now becomes: how can I
(in the function script) cancel the keystroke that occurred. (I'm
coming from a Visual basic perspective here - in VB cancelling a
keystroke is trivial). Is this possible in Javascript?


Instead of trying to cancel the keystroke, put a setTimeout call on your
focus(). That way, it has time to move to the next field, then the
script will set focus where you want it.

Instead of:
document.getElementById(fldID).focus();

Use something like this:

var myVar = null;

And then inside your function, set myVar = to the fieldID:

myVar = fldID;

And then:
myVar = setTimeout(setFocus,1000)
And then:

function setFocus(){
document.getElementById(fldID).focus();
}

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq
Jul 23 '05 #6

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

Similar topics

2
by: TSK | last post by:
I cannot get my focus() fn to work in my validateInput(userInput) fn. It will not allow the user to go back and correct invalid input like it should. Instead it goes right on through with the...
4
by: Scott Navarre | last post by:
Hi, I have Red Hat 8.0 and have the default Mozilla browser that comes with it. I am programming in javascript and have come across something problematic. Given the following code: <HTML>...
4
by: Nitin | last post by:
Hi I have created function to check date and time. at the time of execution, if date is left empty the function returns the error message but then the focus goes to next field. Next filed is for...
8
by: geovanyayala | last post by:
How can i set the focus to one element on my page passing by parameters the form and the field... i search one function like this but y can`t find it... thanks for yoor help..
8
by: Brad Isaacs | last post by:
Good morning friends, I am working with Visual Studio 2005, ASP.NET 2.0 I am working with the Login controls provided my .NET 2.0, trying to make the Login1 control UserName textbox obtain...
5
by: Ansuiya | last post by:
valid = true if ( document.contact_form.contact_name.value == "" ) { alert ( "Please fill in the 'Your Name' box." ); valid = false; document.contact_form.contact_name.focus(); } else...
1
by: Grimm | last post by:
I am developing an internalk inteface that integrates alot of seperate tools into one interface. The current version uses one Iframe inside a div layer for positioning. Currently the seperate web...
2
by: Jonathan N. Little | last post by:
As part of a JavaScript precheck form validation I noticed a problem with trying to return focus to the field with an error. I have setup a demo page. ...
2
by: Alexei Prada | last post by:
Hi guys, I'm not really advanced in html or css, and I'm having a problem that is really getting me out of my nerves. So I redesigned my portfolio site with dreamweaver as my main html css editor....
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
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
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...
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
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

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.