5

Setting focus on a form element using JavaScript is usually very straight forward. I cannot get this working in Firefox 12.0 or Opera 11.

Works in other browsers (Chrome, IE etc) and Firefox 3.6.

Simple HTML:

<form action="">
    <input type="radio" id="focusID1" name="sex" value="male" /> Male<br />
    <input type="radio" id="focusID2" name="sex" value="female" /> Female<br />
</form>

​Simple Javascript:

var elem = document.getElementById("focusID2");
if (elem != null) {
   elem.focus();
}

Try this for yourself at http://jsfiddle.net/4Ddtv/. ​

j.davies
  • 2,321
  • 1
  • 19
  • 24
  • Worksforme at http://jsfiddle.net/4Ddtv/embedded/result/. The code editor also will become focused. Simplified fiddle: http://jsfiddle.net/4Ddtv/1/ – Bergi May 15 '12 at 22:55
  • @Bergi what browser & version are you using? Your example is nice & simplified but I still have the same issue in Firefox 11. – j.davies May 15 '12 at 23:18
  • 1
    Opera 11. Also works with FF 10. – Bergi May 15 '12 at 23:25
  • Your fiddle works fine for me in Firefox 12: the second radio button ends up focused. – Boris Zbarsky May 17 '12 at 08:02
  • @BorisZbarsky which example are you using? If I run and press space to select the focused radio button http://jsfiddle.net/4Ddtv/ and http://jsfiddle.net/4Ddtv/1 do not work in FF 12 but http://jsfiddle.net/4Ddtv/embedded/result does. All three work for me in Chrome and IE 9 – n00begon May 17 '12 at 21:07
  • I was using http://jsfiddle.net/4Ddtv/ and running it and pressing space selects the focused radio for me in Firefox 12 (on Mac, if that matters). – Boris Zbarsky May 18 '12 at 00:33
  • I've observed the issue in Opera 11.64 for Mac. (despite my previous, and deleted comment). Looking into it now. – Tom Pietrosanti May 20 '12 at 03:20
  • Nevermind... it is working... I just forgot to click "Run"... – Tom Pietrosanti May 20 '12 at 03:23
  • All 3 links work for me in Fx12 (Win7 Pro x64), except background-color on a radio, of course (but it still gets focus) – FelipeAls May 20 '12 at 13:07

1 Answers1

2

Firefox was working as expected. Issue was with jsfiddle focus and firefox not highlighting the focused element.

This is not an issue.. User error!

j.davies
  • 2,321
  • 1
  • 19
  • 24