39

I have seen similar questions on SO, including this one, which is old. I read and followed links, but it is unclear whether there is a proper solution to this issue today.

My bottom issue is that I am using HTML's placeholder="..." on the input fields. By focusing automatically on the first field, its placeholder is not visible to the user anymore.

EDIT

Here is my HTML code:

<div id='LOGIN_FORM' title="Login">
    <form action="">
        <input type="text" name="login_id" required="required"
                           placeholder="Enter user ID" /><br />
        <input type="password" name="login_pwd" required="required"
                           placeholder="Enter password" /><br />
    </form>
</div>

Here is my JS code:

$("#login").click(function() { 
    $("#LOGIN_FORM").dialog({ modal: true }, { buttons: [
    {
            text: "Ok",
            click: function() { $(this).dialog("close"); }
        }
    ] });
});
Community
  • 1
  • 1
Jérôme Verstrynge
  • 57,710
  • 92
  • 283
  • 453

10 Answers10

24

What I did was I created an extra input and made it invisible (style="display:none") then gave it the property autofocus="true" put this at the end of your dialog content so it wont mess with anything. it should look like this:

        <div><!--dialog div-->
           <button></button>
           <button></button> 
          <input type="hidden" autofocus="true" />
        </div>
Rudi Kershaw
  • 12,332
  • 7
  • 52
  • 77
Tyler Richardson
  • 279
  • 1
  • 4
  • 11
  • 2
    I followed your solution but it has one great disadvantage - if you bind a _blur_ event to the first input on form (the one that had been auto-focused before) it will be fired right after page loads (try with alert() or console.log()). To avoid this type of issues try to use input of _text_ type instead of _hidden_, ie.: ``. – Kamil Szymański Apr 19 '15 at 23:24
  • 1
    I also had another issue with Chrome, it works first time, but when I close the dialog and reopen it, it ignores it – Ayyash Apr 22 '15 at 16:07
  • Thanks buddy. You saved my day. – dhruv jadia Mar 23 '18 at 06:56
12

A solution is to set tabindex="-1" on ALL input fields to keep HTML placeholders visible.

Jérôme Verstrynge
  • 57,710
  • 92
  • 283
  • 453
11

Adding this tag as the first input field on the page works for me.

<input type="text" autofocus="autofocus" style="display:none" />

No need for javascript and keeps the tab order if you want to use the tab key to move through the fields.

(Tested on Chrome > 65, Firefox > 59 and Edge)

Miguel
  • 1,575
  • 1
  • 27
  • 31
8

I ended up doing this:

<input type="text" style="position: fixed; left: -10000000px;" disabled/>
Robert
  • 667
  • 7
  • 15
  • 1
    putting this as first field works for me, it doesn't fire a blur on the other real fields – Ayyash Apr 22 '15 at 16:16
5

In my case solution with display: none or type=hidden did not help. You can use

<div style="max-width: 0; max-height: 0; overflow: hidden;">
    <input autofocus="true" />
</div>

as first input.

emp
  • 117
  • 2
  • 6
2

Set tabindex=0 on the form. This fixes your issue and should be fine as far as accessibility is concerned.

Neil Girardi
  • 4,533
  • 1
  • 28
  • 45
1

You can use jquery:

jQuery(document).ready(function(e) {
   $('input[name="login_id"]').blur(); 
});

This if you find it hard to trace the bug why autofocus is set into your first input field of the form. Make sure to insert this code before the closing </body> body tag of your document.

Felix Jr
  • 402
  • 6
  • 10
  • problem with this approach is if you have an event listener for onblur on the input (ie, do an auto-search upon blurring the element) – Gonçalo Vieira Aug 28 '17 at 09:07
0

This works for me.

<input type="text" name="fake[email]" style="height:1px;width:1px;overflow:hidden;clip:rect(1px, 1px, 1px, 1px);border:0;outline:none;padding:0;margin:0;color:transparent;" tab-index="-1" aria-hidden="true" value=".">
Mohamad Hamouday
  • 2,070
  • 23
  • 20
0

I solved this issue on my Laravel + Bootstrap setup using below code in my blade template:

{{ Form::text(null, null, ['class' => 'd-none', 'autofocus']) }}
-4

Without JQuery and JavaScript, we can give a CSS-only solution.

  1. remove all focus's outline

    :focus {
        outline: 0;
    }
    
  2. add the new focus's outline

    :link:focus, :visited:focus {
        outline: none;
    }
    
Thomas Fritsch
  • 9,639
  • 33
  • 37
  • 49
Ortsbo
  • 175
  • 1
  • 7
  • Although this will "appear" to resolve the issue, it creates several more accessibility issues :) http://www.outlinenone.com/ – mikestreety Mar 07 '18 at 12:35