Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

Show Me More Code > Manipulating the DOM

Manipulating the DOM

Revisiting the DOM to make it more Dart-esque has a lot of advantages. You can use elements and nodes as actual Dart objects, and you can loop through child nodes just like you can with other Dart collections.

For example, here’s some code that finds a specific element and then performs various operations on the element and its children.

#import("dart:html");

main() {
  // Find an element.
  var elem = document.query("#id");

  // Handle events.
  elem.on.click.add((event) => print('click!'));

  // Set an attribute.
  elem.attributes['name'] = 'value';

  // Add a child element.
  elem.elements.add(new Element.tag("p"));

  // Add a CSS class to each child element.
  elem.elements.forEach((e) => e.classes.add("important"));
  }

  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial