Cannot Focus on Radio Button w/JavaScript focus() Method

Mindwatering Incorporated

Author: Tripp W Black

Created: 10/10/2011 at 04:15 PM

 

Category:
Notes Developer Tips
JavaScript

Issue:
With the following code snippet, the field.focus() method doesn't work. The browser responds that the object doesn't support (have) this method.

f=document._MyForm;
if (f.Q2 !=null) {
f.Q2.focus();
}


Solution:
The solution is to remember that RadioButtons are an array and you have to focus on a specific element within the array to focus. To fix this is actually very simple:
f=document._MyForm;
if (f.Q2 !=null) {
f.Q2[0].focus();
}


Note:
IE seems to give you NO visual clue (As of IE 6 and IE 7) that your focus worked unless the page is long enough to make it scroll. The other good browsers such as Firefox, Safari, and Chrome all block/square the field to let you know that it has focus.


previous page