# Reason for the "Elm way" of retrieving HTML input values?

**URL:** https://discourse.elm-lang.org/t/reason-for-the-elm-way-of-retrieving-html-input-values/2401
**Category:** Learn
**Created:** [November 1, 2018, 3:29am UTC](https://discourse.elm-lang.org/t/reason-for-the-elm-way-of-retrieving-html-input-values/2401 "2018-11-01T03:29:46Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![edemond](https://avatars.discourse-cdn.com/v4/letter/e/74df32/32.png) [@edemond](https://discourse.elm-lang.org/u/edemond)
#### Post date: [November 1, 2018, 3:29am UTC](https://discourse.elm-lang.org/t/reason-for-the-elm-way-of-retrieving-html-input-values/2401/1 "2018-11-01T03:29:46Z")

</div>

New to Elm, sorry if this has been covered, but haven’t found much discussion.

tl;dr: What’s the design rationale (or technical reason) for not being able to directly retrieve the `value` attribute of an HTML input, e.g. through some kind of getter function working on an `Html` instance?

Say my view has something like:

```auto
input [] [],
button [] [text "Submit"]

```

When I click the button, I’d like to do something with the `input` value. I assumed at first it’d work something like:

```auto
let
  myInput = input [] []
in
button [onClick (DoStuffWith (getValue myInput))] [text "Submit"]

```

I’m aware from a couple StackOverflow questions that stashing `input` values in the model is “the Elm way”, but curious to know why. Is it a limitation of the HTML library, or of the language itself that I haven’t grasped yet, or a more intentional design decision?

---

<div class="post-metadata">

### Author: ![norpan](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/norpan/32/548_2.png) [@norpan](https://discourse.elm-lang.org/u/norpan)
#### Post date: [November 1, 2018, 6:32am UTC](https://discourse.elm-lang.org/t/reason-for-the-elm-way-of-retrieving-html-input-values/2401/2 "2018-11-01T06:32:03Z")

</div>

What do you want to do with the input value? Elm has a specific way of handling use interaction (and all other interactions to), see [https://guide.elm-lang.org/architecture/](https://guide.elm-lang.org/architecture/)

---

<div class="post-metadata">

### Author: ![pdamoc](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/pdamoc/32/36_2.png) [@pdamoc](https://discourse.elm-lang.org/u/pdamoc)
#### Post date: [November 1, 2018, 7:34am UTC](https://discourse.elm-lang.org/t/reason-for-the-elm-way-of-retrieving-html-input-values/2401/3 "2018-11-01T07:34:22Z")

</div>

> [@edemond](#):
>
> I’m aware from a couple StackOverflow questions that stashing `input` values in the model is “the Elm way”, but curious to know why.

It is more of an intentional decision and it is related to how Elm views state.

The Html functions are conceptually pure. Of course, the actual nodes that they produce are still stateful due to the underlying implementation but as far as Elm is concerned, they are considered stateless.

So, `input [] []`, from Elm’s perspective, is an empty input field that is always empty.

`getValue myInput` makes not sense (conceptually) in Elm since you give `myInput` the value that it holds (conceptually).

Note that for :

```elm
input [] [],
button [] [text "Submit"]

```

you can still extract the value of the `input` using decoders and the fact that it is the `previousSibling` of the `button` BUT that is not advisable. It would be the equivalent of breaking encapsulation and relying on some internal representation that might change.

---

<div class="post-metadata">

### Author: ![joelq](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/joelq/32/445_2.png) [@joelq](https://discourse.elm-lang.org/u/joelq)
#### Post date: [November 1, 2018, 2:46pm UTC](https://discourse.elm-lang.org/t/reason-for-the-elm-way-of-retrieving-html-input-values/2401/4 "2018-11-01T14:46:54Z")

</div>

It’s also worth noting that all values in Elm are immutable. `myInput` is not a reference to a value that can change over time. This means that even if you had a function `getValue : Html a -> String`, calling `getValue myInput` would always give you an empty string, no matter what the user typed into the input.

More conceptually, Elm view functions don’t have references to the actual DOM. Instead, they construct new (virtual) nodes based on the arguments you give the function. This means the only source of information for a view function are its arguments. It cannot query the DOM.

---

<div class="post-metadata">

### Author: ![malaire](https://avatars.discourse-cdn.com/v4/letter/m/b782af/32.png) [@malaire](https://discourse.elm-lang.org/u/malaire)
#### Post date: [November 1, 2018, 2:55pm UTC](https://discourse.elm-lang.org/t/reason-for-the-elm-way-of-retrieving-html-input-values/2401/5 "2018-11-01T14:55:25Z")

</div>

> [@joelq](#):
>
> This means the only source of information for a view function are its arguments. It cannot query the DOM.

And this is of course true for all functions in Elm - only source of information for any function are its arguments.

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex035/uploads/elm_lang/original/1X/50a05e53677a2c3b47776d7abd0f113eb50193a1.png) [@system](https://discourse.elm-lang.org/u/system)
#### Post date: [November 11, 2018, 2:55pm UTC](https://discourse.elm-lang.org/t/reason-for-the-elm-way-of-retrieving-html-input-values/2401/6 "2018-11-11T14:55:26Z")

</div>

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.
