Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Elm onSelect decoder
-- Assuming we have a list of items in the model (type alias Model = { items : List Item }
-- where Item is a record like { id : Int, name : String }
-- this goes in the view and generates an html dropdown
[ onSelect ValueSelectedMsg ]
( (\item -> option [ value (toString ] [ text ]) model.items)
targetSelectedIndex : Json.Decoder Int
targetSelectedIndex = [ "target", "selectedIndex" ]
onSelect : (Int -> msg) -> Html.Attribute msg
onSelect msg =
on "change" ( msg targetSelectedIndex)
-- This will send a message containing the selected index (eg. ValueSelectedMsg 1)

This comment has been minimized.

Copy link

@robert-hardy robert-hardy commented Nov 3, 2016

Thanks for this.
Got here from this SO answer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.