• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Want to set focus on a Field

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Chris,
Thank You.
I tried the first possibility and it was working fine...
 
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic