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

How to autofocus an input in a Phoenix Form? #138

Open
nelsonic opened this issue May 24, 2020 · 0 comments
Open

How to autofocus an input in a Phoenix Form? #138

nelsonic opened this issue May 24, 2020 · 0 comments

Comments

@nelsonic
Copy link
Member

If you are writing a "traditional" Phoenix (without LiveView) App
and want to autofocus an input this works:

<%= text_input f, autofocus: "true" %>

Note: the value of can be anything e.g: autofocus: "autofocus" works too as does autofocus: "" (empty string). We just prefer the value "true" for clarity in our code.

Whereas our 3 previous attempts don't work:

<%= text_input f, :autofocus %> # using an atom fails 
<%= text_input f, autofocus %> # using a keyword fails
<%= text_input f, "autofocus" %> # using a string fails

We read through https://hexdocs.pm/phoenix_html/Phoenix.HTML.Form.html and did not find any reference to autofocus ... we read through https://elixirforum.com/t/phoenix-liveview-callbacks-to-put-text-input-focus-on-the-selected-input-field/21148 but that is specific to views rendered with LiveView.

If anyone is building a "traditional" Phoenix App as we are in https://github.com/dwyl/phoenix-todo-list-tutorial then this is the method that works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant