Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elm: clear form on submit

Tags:

elm

I have a simple form with one field. I would like to clear the field on form submit. I am clearing my model in my update function, but text remains in the text input.

type alias Model =
    { currentSpelling : String }

type Msg
    = MorePlease

update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
    case msg of
        MorePlease ->
            (  log "cleared spelling: " { model | currentSpelling = "" }
                , fetchWord model.currentSpelling )

view : Model -> Html Msg
view model =
    div []
        [ Html.form [ onSubmit MorePlease ]
            [ input [ type' "text"
                    , placeholder "Search for your word here"
                    , onInput NewSpelling
                    , attribute "autofocus" ""
                    ] []
            , text model.currentSpelling
            , input [ type' "submit" ] [ text "submit!" ]
            ]
        ]

The text displaying model.currentSpelling clears out when I empty it with the update function, but the text input box remains populated. Any idea how to clear it?

fetchWord makes an HTTP call, but it's omitted here.

like image 451
Charlie Avatar asked Oct 17 '16 20:10

Charlie


1 Answers

add value model.currentSpelling into Attributes of the input element. That's how you can control the string inside of input element in html.

like image 160
Tosh Avatar answered Oct 19 '22 11:10

Tosh