Olical Olical - 3 months ago 11
Javascript Question

Focus works in all browsers except IE6?

Hey, just a quick question, I was using the focus function on elements and it seemed to be working fine because it was working in IE7, IE8 and all of the good browsers. When I tried it in IE6...nothing. It is a very basic line.

element.focus();


VERY basic. So does IE6 have a bug or problem with this function? I know the element object exists, I have tested for all of that and it works in all other browsers.

Thanks for any help you can give.

Answer

Are you creating the <input> element dynamically? Stackoverflow might already have the answer.

Update

Running Windows XP SP3 with IE6, I wasn't able to duplicate the issue you describe with the following file:

<html>
<head>
<script type="text/javascript">
function body_load() {
  document.getElementById("field2").focus(); }
</script>
</head>
<body onload="body_load();">
<input type="text" id="field1"/>
<input type="text" id="field2"/>
</body>
</html>

It always put focus on field2 as expected. The following file works as expected too:

<html>
<body>
<input type="text" id="field1"/>
<input type="text" id="field2"/>
<script type="text/javascript">
  document.getElementById("field2").focus();
</script>
</body>
</html>

Are you using application/javascript as the type for your <script>? I know IE6 has issues with that. Perhaps posting an example that can duplicate the issue will help determine why it's not working.

Comments