Adding Things

Although Dart supports the usual Element and Node classes for manipulating the DOM, Dart goes out of its way to allow us to stick to just an Element class. For just about any HTML element, Dart defines a convenience constructor. To create a <div>, for instance, there is the DivElement constructor. Coupled with text and innerHtml properties, it’s easy to get started with DOM elements.

dom/adding.dart
 
var​ gallery = ​new​ DivElement();
 
gallery.text = ​'Welcome to the Gallery'​;

For a more general purpose way to create a new element in Dart, we can use the handy html named constructor for Element.

 
var​ gallery = ​new​ Element.html(​'<div id="gallery">'​);

The Element.html() named constructor does not restrict us to creating a ...

Get Dart 1 for Everyone now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.