Ember.js: Input autofocus only works on first render

Created on 21 Apr 2016  路  6Comments  路  Source: emberjs/ember.js

https://ember-twiddle.com/f5b03da876189e65370402daca33e56c?openFiles=controllers.application.js%2C

Steps to replicate:

  • Click on autofocus once, the input is correctly autofocused
  • Click twice, it is not anymore (unless app is reloaded)
Bug Has Reproduction

Most helpful comment

The "problem" is that autofocus is applied when on page load event, so it is only applied the first time the page is loaded.

All 6 comments

Confirmed, I'm seeing this behavior too. The issue seems to be that the input once generated doesn't properly re-focus when it is hidden and then shown again ...

I am not sure this is an Ember bug. autofocus behaviour is not that simple to grasp, but this thing happens in the following contexts too:

  • Firefox (it does not autofocus even the first time)
  • Simple jQuery adding and removing html shows the same problem (jsbin)
  • Same example but with just <input autofocus> (twiddle)

I am not sure this is a bug and, if it is, that is on our side.

I am closing the issues for the reasons above. Feel free to reopen if you still think this is an Ember issue.

Thank you!

Experienced the same problem here.
Couldn't find better solution than calling
this.$('input').focus()
inside didInsertElement() hook.

The "problem" is that autofocus is applied when on page load event, so it is only applied the first time the page is loaded.

I solved it like this without Jquery:
this.get('element').querySelector('.selector').focus()
You can put it, for example in the didInsertElement hook.

Was this page helpful?
0 / 5 - 0 ratings