DOM Elements

by mithril

Level: beginner • Mithril.js Version: latest

This is a basic example showing Mithril.js' m() function to describe any HTML structure you want. So, here we see some markup together with a CSS class on the title element. This example was taken from the official website at https://mithril.js.org/index.html#dom-elements.

Live Example

Dependencies

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

HTML

<!doctype html>
<html lang=en>
<head>
  <meta charset=utf-8>
  <title>DOM Elements</title>
</head>
<body>
</body>
</html>

JavaScript

let root = document.body
m.render(root, [
  m("main", [
    m("h1", {class: "title"}, "My first app"),
    m("button", "A button"),
  ])
])

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.render API method is use, besides the basic hyperscript function m().

The example was written by mithril, last edits were made on 16 October 2021. 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