The dart:html library provides a set of functions and classes to access DOM nodes. But, sometimes, making the most simple and common operations results in a counterintuitive experience. For instance, let's say that you want to 'prepend' some element into another element:

// prepends element0 into element1
final element0 = querySelector('#element0');
final element1 = querySelector('#element1');
if (element1.hasChildNodes()) {
  element1.insertBefore(element0, element1.childNodes.first);
} else {

Using this library the previous code can be written as follows:

// prepends element0 into element1
final element0 = find('#element0');
final element1 = find('#element1');
element1.appendElement(element0, prepend: true);

The above example is only one of many examples in which the code can be drastically simplified. In any case, I would like to remark that this library does not pretend to replace the official Dart's library. But in most of cases you won't need to use it.


Edit your pubspec.yaml and add the library dependency:

  # specify the version number, for example ^0.0.1
  domelement: <version number>

Alternatively, you can pull the library from the GitHub repository:

  # specify the tag name, for example v0.0.1 (ref entry can be omitted)
    url: git://
    ref: <tag name>

And then gets the dependencies:

> pub get

Finally, import the library from the source code:

import 'package:domelement/core.dart';

void main() {
  final element = find('#element_id');

Finding elements

Use find() and findAll() to select elements from the current document:

// finds a single element
final p1 = find('#p1');
if (p1 == null) {
  throw 'Item not found';

// finds multiple elements
final items = findAll('p');
for (final item in items) {

Creating elements

Use the $() function to create elements. For example:

// creates an anchor element and appends it to the document's body
final anchor = $('<a />')
  // sets some attributes
  ..attr['href'] = 'http://blah blah blah'
  ..attr['title'] = 'Anchor title'
  // sets some CSS attributes
  ..css['color'] = '#333'
  ..css['font-weight'] = 'bold'
  // sets inner texts
  ..text = 'Click here...';

// creates an element from a previous native element
final root = $(document.documentElement);

Responding to events

Use on(), off() and trigger() to operate events:

// prints an alert when clicking #anchor1
find('#anchor1').on('click', () => print('Click!'));

// dispatches an event and passes some extra info
  ..on('click', (event, data) {
    print('Data: ${data}');
    // by returning 'false' stops the event from bubbling up
    // the event chain and prevents the default action
    // in similar way as jQuery does
    return false;
  ..trigger('click', data: 'Hello there!');


As I mentioned previously this library does not pretend to replace the official Dart's library. It can't cover all cases, even though it covers the most common cases.

The most important limitation is that it operates only over DOM Elements. If you want to operate over other type of nodes (like Texts or Comments), you'd better use the official Dart's library.

Additional info

For additional info, check out the API documentation:



A library to traverse and manipulate DOM documents in an easy and intuitive way.