wood burning stoves 2.0*
The moose likes JSP and the fly likes set focus Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "set focus" Watch "set focus" New topic
Author

set focus

Kinga Cserveny
Greenhorn

Joined: Aug 29, 2001
Posts: 18
Can anyone tell me how can I set the focus to a radio button. I need to set the focus on my jsp pages to the first field. I am using a java script function setFocus(name) to do this. I pass in the name of the field and in the function the following is done.
document.form.name.focus();
the code above does not work with the radios.
Thanks for your help.
Rajendar Goud
Ranch Hand

Joined: Mar 06, 2002
Posts: 220
Hi,
I dont think u got any function by the name
setFocus() in javascript. u got Focus() which helps gain focus on components like Password,TextArea and Text fields. For components like RadioButton,as per my reading,focus is automatically obtained.
Raj
Rajendar Goud
Ranch Hand

Joined: Mar 06, 2002
Posts: 220
Hi,
Correction to the previous reply.what u r using seems to be right.it should obtain focus when u use
document.form.radiobutton.focus();
But u need to be careful that ,the form and
radiobutton u r using in the syntax must be the same names as u used to define them in ur HTML .
if not u can use an array to represent these components present on the screen,like
document.forms[o].elements[o].focus();
where forms[0] indicates the first form and
elements[o] indicates the first element on the screen.
Raj
Kinga Cserveny
Greenhorn

Joined: Aug 29, 2001
Posts: 18
thanks for your help. THe java script error that comes up seems to be telling me that radio button does not have the .focus() method. It works with all the other fields. But not with the radio button. I am sure that the name is correct. Any other suggestions?
Thanks for your help
Rajendar Goud
Ranch Hand

Joined: Mar 06, 2002
Posts: 220
Hi
can u send me that script code ?
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16019
    
  20

Technically, a radio button is a member of a group of radio button controls, so setting the FOCUS isn't proper - most GUIs expect that the same mechanism that give focus to the radio button also is going to select it, and that might not be wise.
More properly, one should select the desired radio button and give focus to the group, which should then give what amounts to a sub-focus to the selected radio button.
Most GUIs understand focus on compound controls. Java Swing, alas, does not . For specifics on JavaScript, I'd have to RTFM - the above is based on the abstract concept of focus, not the particular implementations.


Customer surveys are for companies who didn't pay proper attention to begin with.
Kinga Cserveny
Greenhorn

Joined: Aug 29, 2001
Posts: 18
Thanks for your help, unfortunately I did not quite understand how I would be albe to set the focus for the group of radios.
I am not using swing.

Thanks
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
As tim mentioned. Setting the focus to a radio button will automatically select it. And that's proabably why the it's not working. You may try posting to the Javascript/HTML forum for more help.


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Kinga Cserveny
Greenhorn

Joined: Aug 29, 2001
Posts: 18
Sorry Iam finding it impossible to insert the code into the message
Rajendar Goud
Ranch Hand

Joined: Mar 06, 2002
Posts: 220
hi,
If u want to see whether the radio button is checked or not ,then u can use
document.form.radio.checked = true.
ifs that what u need focus for ,then u can get it with this also....But ,it would have been useful,if we had a look at ur code,whats ur actually trying to do ..
Raj
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: set focus