Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
|
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 3rd, 2005, 09:43 PM
Authorized User
 
Join Date: Jun 2003
Posts: 53
Thanks: 0
Thanked 0 Times in 0 Posts
Default Text object methods focus() and select()

Hi:

I have what is probably a simple task, but I haven't succeeded at it yet.

Form validation, checking to ensure that there is a value in the text box using a javascript function in the onblur() event. Works fine.

What I want to do is to return focus to the text field on the form that has failed validation.

Here is a simplified version of my code:



function checkValueExists(strFieldId)

{
    var ctrlObject;
    var strFieldName;

    ctrlObject = document.getElementById(strFieldId);

    if(ctrlObject.value == "")
        {
        alert("Please provide a value for this field");
        ctrlObject.focus();
        }

    ctrlObject = null

}

I'm not sure the focus() method does what I think it should. In operation, if I tab out of the text field after clearing the value, I get the alert message but the focus goes to the next element on the form.

Any help is always appreciated.

Thanks,
JK
 
Old May 3rd, 2005, 11:26 PM
Authorized User
 
Join Date: Jun 2003
Posts: 53
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi:

This looks like it's browser specific.

The behavior I described is what I saw in FireFox 0.8.

In Explorer 6, it works like I expected it would.
 
Old May 3rd, 2005, 11:40 PM
Authorized User
 
Join Date: Jun 2003
Posts: 53
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi:

Downloaded and installed FireFox 1.0.3

Works the same as FireFox 0.8.

Bummer.

JK
 
Old May 4th, 2005, 01:18 AM
Friend of Wrox
 
Join Date: Apr 2005
Posts: 186
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I dont have firefox here, But I remember an old VB5 trick
of putting the focus fist and then the alert.
ie.,

ctrlObject.focus();
alert("Please provide a value for this field");

Let me know did it work!

Prashant


 
Old May 4th, 2005, 02:05 AM
Authorized User
 
Join Date: Jun 2003
Posts: 53
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello Prashant:

Thanks for the tip. Unfortunately, it did not help.

I decided to deal with the problem by restoring the original value to the text object after finding it was cleared when the blur() event occurs.

It's not a bad work around, which considering the fact that I can't think of another one, is a good thing.

John
 
Old May 7th, 2005, 07:18 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Both focus and select work for me in the latest Mozilla browser. The only difference is that in Mozilla the caret is placed at the end of the text after focus.
What are you trying to accomplish with
Code:
ctrlObject = null
?


--

Joe (Microsoft MVP - XML)
 
Old May 8th, 2005, 11:02 PM
Authorized User
 
Join Date: Jun 2003
Posts: 53
Thanks: 0
Thanked 0 Times in 0 Posts
Default


Hi Joe:

Sorry, I'm not seeing it work. I just removed FireFox, Mozilla, and Netscape browsers from my machine. I downloaded Mozilla and installed the entire suite, so I now have Mozilla 1.7.7 installed and I'm seeing the same behavior I did in FireFox 0.8 and 1.0.3.

Delete the value in the text control and tab out.

The onblur() event fires a javascript function that checks the text control for a value, and failing to find one, restores the original value from a hidden control. It then should put up the alert message and return focus to that text control. It does all that with the exception of the focus and selection. The focus goes to next control on the form. In this case, a check box.

I have tried it without restoring the value to the text control from a hidden control, and saw no difference.

In Explorer 6.0.2800.1106, it works exactly like I expect, with focus returned to the text control and the restored value selected for editing. But not with Mozilla, nor with FireFox.

As for

ctrlObject = null

I am eliminating a variable that references an object (ctrlObject = document.getElementById(strFieldId);) which I no longer require, and freeing memory.

Is that not correct?

Although I did notice that I forgot a terminating semi-colon on that line, so it really should be

ctrlObject = null;

if that does what I understand it to do.


Thanks,
John
 
Old May 9th, 2005, 03:12 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

You're right. It doesn't work the same when called using "onblur", I was testing using a button to call the function and this does work. I shall play a little more.


--

Joe (Microsoft MVP - XML)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Setting Text Box Focus sathish_reddy ASP.NET 1.0 and 1.1 Professional 1 November 22nd, 2006 02:26 AM
Object types and methods deeraj Oracle 0 February 2nd, 2006 08:13 AM
focus and select crmpicco Javascript How-To 4 October 4th, 2005 03:24 AM
Properties/Methods Not in Object Browser ritag VS.NET 2002/2003 0 January 13th, 2005 12:59 PM
Get the object with input focus edwardforgacs Javascript How-To 2 December 1st, 2003 05:40 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.