Open BobDankert opened 7 years ago
If anyone has any ideas on how to make this work, that would be appreciated. Right now, I am doing a giant hack:
$timeout(function () {
angular.element(document.getElementById('autocomplete-element-id')).find('input').focus();
}, 1500);
This delay is horrible and causes weird behavior on the page, but it's the best I was able to come up with after trying a number of different approaches.
I'm also having this issue, not yet fixed 👎
I can't seem to get md-autofocus
to work for inputs or autocompletes on CodePen during page load.
I can confirm that md-autofocus
is working with Dialogs and SideNavs (used in the demos) but I can't find a case anywhere that shows it working on page load atm. The docs do state "or upon page load for input-like elements". I've tested with both autocomplete and inputs. Neither gets focus on page load. I tested with CodePen in Debug View and using the docs site running locally.
Testing with Chrome 63 on macOS.
Any update on this?
@killboard nope. PRs are welcome.
@marosoft do you have any interest in looking into this one?
@Splaktar is there any working CodePen I can play with? I can adjust the original one as it is missing all the links to the scripts and css. The one you posted in 2017 expired.
@marosoft this is the editor to the debug view of the CodePen that I posted.
I'm starting to think that this never worked. I tested with 1.0.0, 1.1.0, 1.1.4, 1.1.5, and 1.1.24 but did not see this work on page load w/o a dialog or sidenav in any case.
This CodePen demonstrates the use of autofocus
on an <input>
. The behavior is defined by the browser.
Note that there are a number of caveats with using autofocus
, especially around a11y.
Let's remove "or upon page load for input-like elements" from https://material.angularjs.org/latest/api/directive/mdAutofocus.
We may also want to mention the autofocus
attribute and link to https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#htmlattrdefautofocus from our docs.
@Splaktar you are right about it. It never worked that way. The only thing it does is to make sure that the input field is going to get the focus. This is useful for the scenarios where you want to get the focus like in the dialog.
I created my version of a Codepen based on yours. I moved the md-autofocus
to the md-autocomplete
.
The way I made it to work was to find the focus target in $document
. Part of the code reused from the dialog
component from here.
$timeout(function () {
var target = $mdUtil.findFocusTarget($document);
target.focus();
}, 1500);
A non-related thing is that there was a bug in createFilterFor
and the items were not being displayed.
I tried to use autofocus
attribute in the Codepen but it never worked for me. Even in a demo view.
Actual Behavior:
What is the issue? *
Adding md-autofocus to an autocomplete does not set focus on the elementWhat is the expected behavior?
It is expected it would set focus on the elementCodePen (or steps to reproduce the issue): *
CodePen Demo which shows your issue:
https://codepen.io/anon/pen/mqyjjzDetails:
I added md-autofocus to the md-autocomplete element from the demo on the angular material website under md-autocomplete. The item does not get focused.AngularJS Versions: *
AngularJS Version:
1.6.6AngularJS Material Version:
1.1.5Additional Information:
Browser Type: *
ChromeBrowser Version: *
61.0.3163.100 (Official Build) (64-bit)OS: *
Windows 10Stack Traces:
Shortcut to create a new CodePen Demo. Note:
*
indicates required information. Without this information, your issue may be auto-closed.