Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old May 3rd, 2005, 09:43 PM
Authorized User
 
Join Date: Jun 2003
Location: Sheridan, OR, USA.
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
Reply With Quote
  #2 (permalink)  
Old May 3rd, 2005, 11:26 PM
Authorized User
 
Join Date: Jun 2003
Location: Sheridan, OR, USA.
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.
Reply With Quote
  #3 (permalink)  
Old May 3rd, 2005, 11:40 PM
Authorized User
 
Join Date: Jun 2003
Location: Sheridan, OR, USA.
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
Reply With Quote
  #4 (permalink)  
Old May 4th, 2005, 01:18 AM
Friend of Wrox
 
Join Date: Apr 2005
Location: Cochin, , India.
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


Reply With Quote
  #5 (permalink)  
Old May 4th, 2005, 02:05 AM
Authorized User
 
Join Date: Jun 2003
Location: Sheridan, OR, USA.
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
Reply With Quote
  #6 (permalink)  
Old May 7th, 2005, 07:18 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
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)
Reply With Quote
  #7 (permalink)  
Old May 8th, 2005, 11:02 PM
Authorized User
 
Join Date: Jun 2003
Location: Sheridan, OR, USA.
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
Reply With Quote
  #8 (permalink)  
Old May 9th, 2005, 03:12 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
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)
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 02:06 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.