material icon indicating copy to clipboard operation
material copied to clipboard

Autofocus input elements

Open SnakeME opened this issue 11 years ago • 27 comments

Is there a solution to set autofocus for input elements which use the md-input-container?

The following example is not working:

<md-input-container>
  <label>Username</label>
  <input ng-model="user.name" type="text" autofocus>
</md-input-container>

My current workaround is adding focus to the input and the class md-input-focused to md-input-container myself.

SnakeME avatar Oct 23 '15 13:10 SnakeME

I also ran into this and like you ended up setting focus manually, with $timeout, but in my experience calling .focus() was sufficient, I didn't manually add md-input-focused class. If autofocus was supported, would be a nice addition.

ivan7237d avatar Oct 25 '15 22:10 ivan7237d

When I just call .focus() then the focus is set for the input field, but it doesn't have the angular material design (colored bottom border).

SnakeME avatar Oct 26 '15 08:10 SnakeME

Hmm that's strange, because in my case it does get the colored bottom border. As far as I understand, calling focus() should have the same result as for instance tabbing to the input, the focus event should trigger the class change.

ivan7237d avatar Oct 26 '15 17:10 ivan7237d

Isn't this what md-autofocus does? If I am not misunderstanding this the issue should be closed.

<md-input-container>
  <label>Username</label>
  <input ng-model="user.name" type="text" md-autofocus>
</md-input-container>

adrianandreias avatar Jan 14 '16 08:01 adrianandreias

@SnakeME did you got your answer from @adrianandreias? (thanks!)

EladBezalel avatar Jan 14 '16 10:01 EladBezalel

md-autofocus is not working for me (using v1.0.1).

http://codepen.io/anon/pen/zrdmaW

SnakeME avatar Jan 14 '16 10:01 SnakeME

@SnakeME are you trying to use dialog/autocomplete/bottomsheet/sidenav?

maybe we should add support for just input container

EladBezalel avatar Jan 14 '16 10:01 EladBezalel

My case has nothing to do with dialog/autocomplete/bottomsheet/sidenav. I just want to set autofocus for a standart input container. Like this attribute: http://www.w3schools.com/tags/att_input_autofocus.asp

But wether the HTML attribute nor md-autofocus are working.

      <form name="userForm">
        <md-input-container class="md-block">
          <label>Input-autofocus</label>
          <input ng-model="user.firstName" type="text" autofocus>
        </md-input-container>

        <md-input-container class="md-block">
          <label>Input-md-autofocus</label>
          <input ng-model="user.title" type="text" md-autofocus>
        </md-input-container>
      </form>

Both attributes seem to be ignored.

I've updated the codepen http://codepen.io/anon/pen/zrdmaW

SnakeME avatar Jan 14 '16 11:01 SnakeME

I can confirm that md-autofocus is not working in a simple page, but only in dialogs (as described in #6680).

(Initially you asked if there is a solution: there's is one, just that not working due to a bug :) ).

The fix for this bug is probably related to #6679 as well

adrianandreias avatar Jan 14 '16 12:01 adrianandreias

:+1:

mangrish avatar Jan 15 '16 04:01 mangrish

I just ran into this issue and was able to get it to work correctly until a proper fix can be implemented by using md-autofocus="autofocus" instead of md-autofocus alone.

RandyDavis avatar Feb 17 '16 04:02 RandyDavis

Running into an issue here as well. In my case, I tried md-autofocus and md-autofocus="autofocus" on both the input itself and the md-input-container. None of the combinations worked.

Currently I have autofocus on the input. If the site is loaded on the sign-in page (for example), it works as intended. If the home page is loaded and I navigate to the sign-in page, it looks like it focuses then immediately blurs, triggering the validation on the field.

[EDIT] I just realized that the focus-blur thing is only happening when the side nav is hidden (md or smaller). If the browser window is large and the side nav is locked open (gt-md), autofocus works just fine.

I'm using v1.0.5 with Angular v1.5.0.

Kizmar avatar Feb 17 '16 22:02 Kizmar

I want my input field to be focused when i change "md-tab" or md-tab-selected

I tried also md-autofocus and md-autofocus="autofocus" on both the input itself and the md-input-container. None of the combinations worked.

dongorous avatar Mar 30 '16 10:03 dongorous

@EladBezalel - I'm having this problem too. I'm trying to focus an input when loading a simple form, and as @Kizmar, I tried all the combinations without success. I updated @SnakeME's codepen to show that neither autofocus or md-autofocus are working on text inputs or textareas (using Angular 1.4.8 and Angular Material 1.1.0-rc2).

So we are trying to implement a whole form using Angular Material and we keep running into this issues with basic functionality, like we did with https://github.com/angular/material/issues/4233.

It's very frustrating that these basic behaviors aren't working and are not even considered important. I don't get what needs to be discussed on this issue and how a P1 issue is still in the backlog.

TrancaDev avatar Apr 06 '16 15:04 TrancaDev

+1

angryfatkid avatar Apr 06 '16 17:04 angryfatkid

Having the same issue!

+1

johnyricalde avatar Apr 06 '16 18:04 johnyricalde

having the same issue that by calling .focus() the coloured bottom border doesn't appear. It does set the cursor inside the input and works for typing but no material bottom border.

I should mention that this happens on Chrome Version 49.0.2623.110 (64-bit). Works fine when I try it on Safari.

LiyinZ avatar Apr 08 '16 14:04 LiyinZ

I am neither able to set or remove focus from mddialog input element on mobile browsers. When the dialog is shown it randomly focuses on any one element, during which time the keyboard tries to come up and the dialog repositions,but then the focus goes away and the keyboard disappears and the dialog stays without focus.

So the whole interaction is very jerky currently. is there a fix for either forcing focus on one element so the keyboard stays either open or close?

srinivasmangipudi avatar Apr 11 '16 15:04 srinivasmangipudi

@srinivasmangipudi - I'm using this workaround for setting the focus upon activation. I haven't tested on mobile browsers though.

function setDefaultFocus() {
    if (scope.activated) {
        angular.element('textarea[name=name]').focus();
    } else {
        $timeout(function () {
            setDefaultFocus();
        }, 300);
    }
}

TrancaDev avatar Apr 13 '16 19:04 TrancaDev

I'm having an issue using ng-repeat and md-bottomsheet. I'm repeating a series of cards and each card has a bottom sheet. When I activate the bottomsheet on one card, it only ever pulls open the bottomsheet on the first card.

I believe the error has to do with my mis-use of md-autofocus. Let me know if I should open a new thread. Thanks

smbowler avatar Apr 29 '16 20:04 smbowler