| Author |
Want to set focus on a Field
|
D.Hemanth Kumar
Greenhorn
Joined: Apr 23, 2004
Posts: 3
|
|
Hi, Can u please anyone help me in this regard. I am not sure whether it is easy or tough, as i am new to javascript. I want to set focus to the user field when the page loads... Here is the code I used.. body onnLoad="document.loginForm.[User Name].focus();" In the form tag.. input size="25" name="User Name" I want to get focussed to the User name field. I know this will work if i give the name as UserName as a single word. But i want to use as User Name and give focus to it.
|
Hemanth Kumar
|
 |
Chris Baron
Ranch Hand
Joined: Mar 21, 2003
Posts: 1028
|
|
Hi, two possibilities i know: 1. document.forms[0].elements[1].focus() sets the focus to the second field in the first form. 2. document.getElementById('User Name').focus() works also, but not in older browsers. You also have to use "id" instead of "name" (usage of both is possible) in the input-tag. cb [ April 26, 2004: Message edited by: Chris Baron ]
|
 |
D.Hemanth Kumar
Greenhorn
Joined: Apr 23, 2004
Posts: 3
|
|
Hi Chris, Thank You. I tried the first possibility and it was working fine...
|
 |
Bear Bibeault
Author and opinionated walrus
Marshal
Joined: Jan 10, 2002
Posts: 50677
|
|
I would not recommend using the numeric references. They're hard to read and will "break" if you change your page around. Your original example would work if you remember to quote the element name: [ April 26, 2004: Message edited by: Bear Bibeault ]
|
[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
|
 |
Eric Pascarello
author
Rancher
Joined: Nov 08, 2001
Posts: 15003
|
|
Lets point out what was wrong with your line of code. onnLoad="document.loginForm.[User Name].focus();" you are referencing the element array wrong by using a peroid. you are making the script look for a variable User with an Error behind it. So if we get rid of the peroid and change the variable to a string we get this: onnLoad="document.loginForm['User Name'].focus();" Referencing form elements by number is not a good way to do it, since you have to worry about adding other elements to the page and other forms. This can cause problems and force you to hae to change more iformation then you actually need to when you update your page. Eric [EDIT: Bear beat me!] [ April 26, 2004: Message edited by: Eric Pascarello ]
|
 |
 |
|
|
subject: Want to set focus on a Field
|
|
|