Setting focus on a newly created input

747 views
Skip to first unread message

Joseph Collard

unread,
Aug 5, 2015, 3:33:02 PM8/5/15
to elm-d...@googlegroups.com
Hi all,

I'm attempting to write a form that allows users to create new input elements. I would like it such that when a the new input is created, that the focus is set to that input. In javascript I would call .focus() on the newly created element. I was hoping to achieve this without writing some crazy hack using ports.

Here is a simple gist containing a button that generates inputs: https://gist.github.com/jcollard/167cb292dc8f203fa66e

Unfortunately, share-elm complains about the imports. However, you can run this on elm-lang.org/try 

Any ideas?

James Byatt

unread,
Aug 5, 2015, 4:27:24 PM8/5/15
to elm-d...@googlegroups.com
Hi Joseph,

Have you tried the autofocus attribute? http://package.elm-lang.org/packages/evancz/elm-html/4.0.1/Html-Attributes#autofocus

Cheers,
James.

--
You received this message because you are subscribed to the Google Groups "Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Joseph Collard

unread,
Aug 5, 2015, 4:28:45 PM8/5/15
to elm-d...@googlegroups.com

I have. It only works when the page initially loads. It does not focus if the element is dynamically creates.

sent from android

Clinton De Young

unread,
Oct 22, 2015, 12:36:38 AM10/22/15
to Elm Discuss
Hi Joseph,

I'm new to Elm and have run into this as well.  Did you ever figure out a clean, Elm way to do it, or did you have to kludge together a port solution?

Alexey Zlobin

unread,
Nov 18, 2015, 11:44:07 AM11/18/15
to elm-d...@googlegroups.com
Hi everyone,

Sorry if this topic already covered elsewhere, but it's the most recent thread.

I've found a solution for the focus problem in the ToDoMVC example.
It's based on a dedicated 'focus' port. Is it the recommended way to
deal with input focus?

Code I've found so far:
- https://github.com/evancz/elm-todomvc/blob/master/Todo.elm#L329
- https://github.com/evancz/elm-todomvc/blob/master/index.html#L19

Alexey.

Pete Vilter

unread,
Nov 19, 2015, 3:48:30 AM11/19/15
to Elm Discuss
This should be doable as a Task. Opened an issue against evancz/elm-html: https://github.com/evancz/elm-html/issues/88

TheGryzor123

unread,
May 26, 2016, 6:04:23 AM5/26/16
to Elm Discuss
I'm having this problem as well. The autofocus only works at the initial load, not after.
Reply all
Reply to author
Forward
0 new messages