7

I am working with and angular JS and HTML5 application. I have a login form with username and password field. when the login page loads i want the focus to be on the username by default.i have used HTML5 autofocus="autofocus" to implement this without writing any script.

 <input name="USER" id="USER" type="email" class="input-control" data-ng-model="login.userName" required autofocus="autofocus">

This works fine in Google Chrome, Internet Explorer and Safari. however the autofocus is not working in Mozilla Firefox.i have red about some issues with autofocus in firefox for old versions. i am using version 48.0

Any solutions? Thanks in advance

Sha
  • 1,826
  • 5
  • 29
  • 53
  • Firefox 48 does not support autofocus [http://caniuse.com/#feat=autofocus](http://caniuse.com/#feat=autofocus). There's a stackoverflow thread for this [http://stackoverflow.com/questions/9955398/autofocus-attribute-of-html5-does-not-work-only-in-firefox-when-forminput-ar](http://stackoverflow.com/questions/9955398/autofocus-attribute-of-html5-does-not-work-only-in-firefox-when-forminput-ar). Or you can use some polyfil like [https://jsfiddle.net/jonathansampson/qZHxv/](https://jsfiddle.net/jonathansampson/qZHxv/) (google it) – phoa Nov 29 '16 at 10:10
  • 1
    @phoa - according to the link you posted firefox has supported autofocus since version 4 - released in March 2011 – Jaromanda X Nov 29 '16 at 11:02
  • @JaromandaX - not sure which link you meant, but the reference from caniuse mentions that autofocus on firefox is supported from v49, not version 4. – phoa Nov 29 '16 at 15:45
  • Click show all to see – Jaromanda X Nov 29 '16 at 20:52
  • @JaromandaX - you're right. my bad. I never paid attention to the show all button there. – phoa Nov 30 '16 at 04:01
  • `JaromandaX - you're right` if I had a nickel for every time someone said that I'd have 35 cents :p – Jaromanda X Nov 30 '16 at 04:03

2 Answers2

2

$('#element').focus() should work

This question has already been asked and answered here: Autofocus Attribute of HTML5 does not work only in FireFox when <Form><input> are loaded via Ajax. WHY? and here: Autofocus doesn't work on Firefox and doesn't work when coming from another page

Community
  • 1
  • 1
ArthurG
  • 335
  • 2
  • 11
0

Why not try using jquery

    <!DOCTYPE html>
    <html>
    <head>
    <title></title>

    <form>
    <input type="text" name="name" id="name">
    </form>
    </head>
<body>
<script type="text/javascript" src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script type="text/javascript">
  $('#name').focus();

</script>
</body>
</html>