Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTML5 <input> autofocus attribute inside modals #12525

Closed
henrywright opened this issue Feb 2, 2014 · 10 comments
Closed

HTML5 <input> autofocus attribute inside modals #12525

henrywright opened this issue Feb 2, 2014 · 10 comments

Comments

@henrywright
Copy link

Focus doesn't seem to happen when using the HTML5 autofocus attribute on a text input when the text input is inside a modal

<input type="text" class="class" name="name" value="" autofocus>

Tested in Chrome 32.0.1700.76 m using Bootstrap v3.1

@hnrch02
Copy link
Collaborator

hnrch02 commented Feb 2, 2014

That's because the modal does not get injected into the DOM dynamically but rather exists the whole time (unless you use the remote option) and is invisible. You can emulate the autofocus attribute by something similar to this.

@hnrch02
Copy link
Collaborator

hnrch02 commented Feb 2, 2014

Of course Bootstrap could implement this by just searching the modal for an input with the autofocus attribute an then, once the modal is shown, setting the focus to that element, but I don't know if the team wants that in the core.

@henrywright
Copy link
Author

@hnrch02 tested the linked-to solution in Chrome and it works great. But doesn't seem to work on iPad.

@hnrch02
Copy link
Collaborator

hnrch02 commented Feb 2, 2014

Triggering focus on iOS seems tricky.

@henrywright
Copy link
Author

Triggering focus on iOS seems tricky.

It does seem to be!

@delight
Copy link

delight commented May 1, 2014

At least about html5-autofocus I know that most mobile browser (besides android-firefox) ignore the autofocus attribute. It appears to be a good idea regarding the usability. In most cases you don't want to cover half the screen / web page with the virtual keyboard in the moment you load the page.

@cvrebert
Copy link
Collaborator

@twbs/team What are folks' thoughts on this?

@cvrebert cvrebert modified the milestones: v3.3.0, v3.2.1 Jun 9, 2014
@fat
Copy link
Member

fat commented Jun 10, 2014

i dont think we should implement this in core for now. People should be able to easily shim this with a global listener (untested):

$(document).on('shown.bs.modal', function (e) {
  $(e.target, '[autofocus]').focus()
})

Triggering focus on iOS seems tricky.

It's also nasty because it fires the virtual keyboard which introduces a whole bunch of sadness and confusion to a user (especially if they are just triggering a modal).

@fat fat closed this as completed Jun 10, 2014
@fat
Copy link
Member

fat commented Jun 10, 2014

it's also one of those things, which might be worth visiting – depending if it comes up a lot – because it's pretty easy to add

@mdo mdo removed this from the v3.3.0 milestone Jun 10, 2014
@pimlottc
Copy link

pimlottc commented Sep 9, 2014

@fat shouldn't that be:

     $(document).on('shown.bs.modal', function (e) {
        $('[autofocus]', e.target).focus();
      });

@twbs twbs locked and limited conversation to collaborators Sep 9, 2014
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

7 participants