This sets the actual contents of the text box. You’re not seeing a difference when you remove it because you’re not doing anything interesting with it anyway. However, this would be really important if, for example, you had a starting input for the text box. Maybe it is say an input box for the user to change their display name or something, so it must first contain the original value.
However, better than me explaining what is going on here, I suggest you make a couple of changes to the program. First do the following, change the initial value of the content on the model:
init : Model
init =
{ content = "Hello" }
Re-compile and have a little play around. Try doing the same thing whilst removing the value model.contentfrom the input.
Then try changing what you do in the update function (make sure you have the value model.content back on the input for these:
update : Msg -> Model -> Model
update msg model =
case msg of
Change newContent ->
{ model | content = newContent ++ newContent}
Or:
update : Msg -> Model -> Model
update msg model =
case msg of
Change newContent ->
{ model | content = String.left 5 newContent }
Or even:
update : Msg -> Model -> Model
update msg model =
case msg of
Change newContent ->
{ model | content = String.reverse newContent }
Keep in mind that setting the value attribute in the input means strange things can happen when you change the value input in the update function and edit not at the end of the text.
For example, change the update function like @allanderek suggested, but instead of typing at the end of the text input, type in the middle, and you will see how after each key stroke the cursor jumps to the end of the input.
Not the most straightforward behavior and a bit of a gotcha.
If the cursor is managed somewhat properly you could be formatting the input as the user types it, for example a phone number or currency, adding spaces and punctuation.
The reality is that the cursor management in those cases can be tricky besides the dumb behavior of “restore it where it was”, so Elm doesn’t deal with it.
I personally don’t use what they call in react “controlled components” for the inputs in Elm, and only subscribe to onInput and update the model, without setting the value back to the input, and it seems to work since the input keeps the state and the events update the elm model.
I remember there was some trickery as well with defaultValue and such but I can’t recall right now what it was. Maybe others have better answers than me