0

I am trying to get the .focus() working in IE, it works in chrome etc. My form is called:

<form name="feedbackform" action="feedback.asp" target="_self" onsubmit="return 
        validate_txt(this)" method="post" style="margin: 0;">

my radio buttons:

<input type="radio" name="fb_commentype" value="Comment" />Comment
<input type="radio" name="fb_commentype" value="Complaint" />Complaint
<input type="radio" name="fb_commentype" value="Request" />Request

in my javascript I am trying to call using this line:

document.forms["feedbackform"].elements["fb_commentype"][0].focus();

As I said, it works in chrome, firefox blah blah blah but in IE 8 I am getting nada, zip and I don't know why, nor can I find a satisfactory answer, is there a way around it?

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
flavour404
  • 6,184
  • 30
  • 105
  • 136

3 Answers3

2
<input type="radio" name="fb_commentype" value="Comment" />Comment
<input type="radio" name="fb_commentype" value="Complaint" />Complaint
<input type="radio" name="fb_commentype" value="Request" />Request

Simple, look at your radio button as an array, you can focus any radio button of the array by pointing to the right index, see below.

document.getElementsByName('fb_commentype')[0].focus();

that way the "Comment" radio button will be focused...

Happy coding!!

0

Are you calling focus when the page just loads (e.g. body's onload)?

This thread might help - it may get called in your code before DOM finished loading

Also, this page has a good test for focus() behavior: http://hardlikesoftware.com/projects/IE8FocusTest.html

Community
  • 1
  • 1
DVK
  • 126,886
  • 32
  • 213
  • 327
  • No, its a check function when the user presses the submit button, validates input and if something is missing sets the focus to that item. – flavour404 Apr 14 '10 at 18:49
0

This might be that IE does not understand the .[0].focus() syntax. You may want to try something like this:

document.forms["feedbackform"].elements["fb_commentype"][0].focus();
document.forms["feedbackform"].elements["fb_commentype"].focus(); 
Thomas Kjørnes
  • 1,928
  • 1
  • 17
  • 17