insertAdjacentHtml method - Element class - dart:html library (original) (raw)

void insertAdjacentHtml(

  1. String where,
  2. String html, {
  3. NodeValidator? validator,
  4. NodeTreeSanitizer? treeSanitizer, })

Parses text as an HTML fragment and inserts it into the DOM at the specified location.

The where parameter indicates where to insert the HTML fragment:

    var html = '<div class="something">content</div>';
    // Inserts as the first child
    document.body.insertAdjacentHtml('afterBegin', html);
    var createdElement = document.body.children[0];
    print(createdElement.classes[0]); // Prints 'something'

See also:

Implementation

void insertAdjacentHtml(
  String where,
  String html, {
  NodeValidator? validator,
  NodeTreeSanitizer? treeSanitizer,
}) {
  if (treeSanitizer is _TrustedHtmlTreeSanitizer) {
    _insertAdjacentHtml(where, html);
  } else {
    _insertAdjacentNode(
      where,
      createFragment(
        html,
        validator: validator,
        treeSanitizer: treeSanitizer,
      ),
    );
  }
}