Skip to content

How do you read the content of an input field with Mint? #569

Answered by gdotdesign
serge-hulne asked this question in Q&A
Discussion options

You must be logged in to vote

This is how you use references (myInput):

component Main {
  fun handleClick (event : Html.Event)  {
    try {
      value = 
        case (myInput) {
          Maybe::Just(element) => Maybe::Just(Dom.getValue(element))
          Maybe::Nothing => Maybe::Nothing
        }

      Window.alert(Maybe.withDefault("", value))
    }
  }

  fun render : Html {
    <div>
      <input as myInput/>
      <button onClick={handleClick}>
        "Get input content"
      </button>
    </div>
  }
}

You can see it working here: https://sandbox.mint-lang.com/sandboxes/EWTecSzsyNdVIg
And here is the documentation page about them: https://mint-lang.com/guide/reference/components/referencing-entities

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by serge-hulne
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants