# Manipulate the DOM Tree

Elemento has static helpers methods in [`Elements`](https://hal.github.io/elemento/apidocs/org/jboss/elemento/Elements.html) that help you to manipulate the DOM tree:

* `Elements.lazyAppend(Element parent, Element child)`\
  Appends `child` to `parent`. If `parent` already contains `child`, this method does nothing.
* `Elements.insertAfter(Element newElement, Element after)`\
  Inserts `newElement` into `after`'s parent after `after`.
* `Elements.lazyInsertAfter(Element newElement, Element after)`\
  Inserts `newElement` into `after`'s parent after `after`. If `after`'s parent already contains `newElement`, this method does nothing.
* `Elements.insertBefore(Element newElement, Element before)`\
  Inserts `newElement` into `before`'s parent before `before`.
* `Elements.lazyInsertBefore(Element new Element, Element before)`\
  Inserts `newElement` into `before`'s parent before `before`. If `before`'s parent already contains `newElement`, this method does nothing.
* `Elements.insertFirst(Element parent, Element newElement)`\
  Inserts `newElement` as the first element into `parent`.
* `Elements.removeChildrenFrom(Element element)`\
  Removes all child elements from `element`
* `Elements.failSafeRemove(Node parent, Element child)`\
  Removes `child` from `parent`.
* `Elements.failSafeRemoveFromParent(Element element)`\
  Removes `element` from its parent.

All methods are null-safe, check parent/child relationships, and are overloaded to accept an instance of `IsElement<Element>` instead of `Element`. See the API documentation of [`Elements`](https://hal.github.io/elemento/apidocs/org/jboss/elemento/Elements.html) for more details.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://hal-console.gitbook.io/elemento/dom.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
