Skip to content

Instantly share code, notes, and snippets.

@pablohirafuji
Created October 22, 2016 09:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pablohirafuji/1fcfec3418c0c17bff0b2fb776d92fe2 to your computer and use it in GitHub Desktop.
Save pablohirafuji/1fcfec3418c0c17bff0b2fb776d92fe2 to your computer and use it in GitHub Desktop.
Elm defaulValue vs. value
import Html exposing (..)
import Html.App exposing (beginnerProgram)
import Html.Attributes exposing (..)
import Html.Events exposing (onInput, onClick)
main =
beginnerProgram { model = Model "" "", view = view, update = update }
type alias Model =
{ defaultValueEg : String
, valueEg : String
}
view model =
div []
[ h1 [] [ text "Using defaultValue" ]
, textarea [ onInput Text1Input, defaultValue model.defaultValueEg ] []
, button [ onClick ResetText1 ] [ text "Reset" ]
, h1 [] [ text "Using value" ]
, textarea [ onInput Text2Input, value model.valueEg ] []
, button [ onClick ResetText2 ] [ text "Reset" ]
, div [] [ text (toString model) ]
]
type Msg
= Text1Input String
| ResetText1
| Text2Input String
| ResetText2
update msg model =
case msg of
Text1Input str ->
{ model | defaultValueEg = str }
ResetText1 ->
{ model | defaultValueEg = "" }
Text2Input str ->
{ model | valueEg = str }
ResetText2 ->
{ model | valueEg = "" }
@danneu
Copy link

danneu commented Aug 6, 2017

The github issue where you link to this gist is at the top of google, so here's my fork that makes the one-liner change so that it works with Elm 0.18: https://gist.github.com/danneu/958d6f39a6e33721dda98b5dddc3a2be

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