Elm on input event

No Comments

Update 05/16/2016: And the stuff below becomes completely irrelevant with Elm 0.17. Just use the new Events.onInput.

————
————
This could be tricky at first, since Elm doesn’t provide (yet) a built-in onInput handler. Rather, you have to use something a little more generic like this:
on “input” targetValue (\v -> Signal.message address v)

This will send user input to the designated address. Or, say if you want to send it to some action, then you could write something like this:

on "input" targetValue (\v -> Signal.message address (action v))

To deconstruct what’s going on here, let’s look at all of the arguments:
“input” – the type of event
targetValue – A Json.Decoder for grabbing ‘event.target.value’ from the triggered event.
(\v -> Signal.message address (action v)) – function that handles the input value

And if you look at the definition of onClick, it basically is a shortcut for on “click”:

messageOn : String -> Signal.Address a -> a -> Attribute
messageOn name addr msg =
on name value (\_ -> Signal.message addr msg)

onClick : Signal.Address a -> a -> Attribute
onClick =
messageOn “click”

Categories: Elm

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Loading Facebook Comments ...