domQuery

An abstraction layer of the DOM implementation that allows us to manipulate and traverse documents in an easy and intuitive way. This library is not intended as a replacement for the DOM implementation. But in most cases you will not need to use native DOM functions.

DomNode methods

  • node.query(String selectors): Finds elements
  • node.attr(String name, [Object value]): gets or sets attributes
  • node.css(String name, [String value]): gets or sets CSS attributes
  • node.text([Object value]): gets or sets inner text
  • node.html([Object value]): gets or sets inner html
  • node.data(String name, [Object value]): gets or sets data
  • node.append(Object obj): appends an object
  • node.prepend(Object obj): prepends an object
  • node.name(): gets the node name
  • node.parent(): gets the parent element
  • node.clean(): removes all child nodes
  • node.remove(): removes the node from the document
  • node.element(): gets the native Element object

Examples

Creating instances

import 'package:domquery/dom/node.dart';

void main() {
  var node;
  
  // creates a node with attributes
  node = new DomNode('item', attributes: {'id': 101, 'title': 'Item title'});
  print(node);
  
  // creates a node with inner text
  node = new DomNode('item', text: 'Lorem ipsum ...');
  print(node);
  
  // creates a node with inner html
  node = new DomNode('item', html: '<span>Lorem ipsum ...</span>');
  print(node);
  
  // creates a node with attributes and child nodes
  node = new DomNode('item', attributes: {'id': 101, 'title': 'Item title'}, callback: (DomNode target) {
    target.append('<node id="1" />');
    target.append('<node id="2" />');
    target.append('<node id="3" />');
  });
  print(node);
}

Traversing documents

You can use the same function domQuery to retrieve single or multiple elements.

import 'package:domquery/utils.dart';

void main() {
  // accessing a single element
  var title = domQuery('h1');
  title.text('Changing title');
  
  // accessing multiple elements
  var paragraphs = domQuery('p');
  paragraphs.forEach((item) {
    item.html('<strong>Paragraph</strong>');
  });
}

Libraries

dom
exception
utils
utils