Select

by osban

Level: beginner • Mithril.js Version: latest

This example shows how to render a normal HTML select element. It also demonstrates how to catch the selected item using the onchange handler.

Live Example

Dependencies

Type Name URL
scriptmithril@latesthttps://unpkg.com/mithril@latest

JavaScript

const app = () => {
  let choice = 'Item2'

  return {
    view: () => [
      m('h1', 'Select'),
      m('select', {
          onchange: e => choice = e.target.value,
          value: choice
        },
        ['Item1', 'Item2', 'Item3'].map(x =>
          m('option', x)
        )
      )
    ]
  }
}

m.mount(document.body, app)

CSS

label {
  display: block
}

As a prerequisite for this snippet, the latest version of Mithril.js framework is required. Beginners should have no problems following this example, that simply shows some basic recipies.

In this code sample Mithril.js' m.mount API method is use, besides the basic hyperscript function m().

The example was written by osban, last edits were made on 24 September 2020. The author has contributed some more snippets. Click here to see them all.

Contribute

Did you note a typo or something else? So let me know by opening an issue. Or much better: just fork the repository on GitHub, push your commits and send a pull request. Ready to start your work? Then click on the edit link below. Thanks in advance!

See more code examples  •  Edit this example on GitHub