---
title: "Move an HTML DOM node to another parent in C#"
url: https://recursiveai.org/onehtml/cookbook/move-dom-node-without-losing-identity/
description: "Move an existing HTML node to another parent with AppendChild in C#, keep its mutable key, and find the same node in the committed snapshot."
version: "1.7.0"
---

# Move an HTML DOM node to another parent in C#

**onehtml** is a free, open-source HTML library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneHtml) · [GitHub](https://github.com/recursive-ai-oss/onehtml-csharp) · [Docs](https://recursiveai.org/onehtml/docs/) · [Download example (.zip)](https://recursiveai.org/onehtml/cookbook/move-dom-node-without-losing-identity/move-dom-node-without-losing-identity.zip)

Call `AppendChild` on the new parent to move the existing node. Look up the element in the read-only snapshot, use `ToMutable` to get an editable handle, and call `Commit()` to make your edits visible in the next snapshot.

```shell
dotnet add package RecursiveAI.OneHtml
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using RecursiveAI.OneHtml.Mutation;

var doc = MutableDocument.Parse(
    "<ul><li>Tea</li></ul>" +
    "<ol id='to'></ol>");
var start = doc.Snapshot();
var target = start.Document.GetElementById("to");
var to = start.ToMutable(target);
var item = to.PreviousSibling.FirstChild;
var key = item.Key;
to.AppendChild(item);
doc.Commit();
Console.WriteLine(doc.SerializeNode(to));
Console.WriteLine(key == item.Key);
```

[The example](https://recursiveai.org/onehtml/cookbook/move-dom-node-without-losing-identity/move-dom-node-without-losing-identity.zip) is the full program below: unzip it and `dotnet run`. To run only the code above, paste it into Program.cs of a new `dotnet new console` project and `dotnet run`.

The new list contains `Tea`, and `True` means the node kept its key:

```text
<ol id="to"><li>Tea</li></ol>
True
```

## The full program

The sample lists are adjacent, so `to.PreviousSibling` is the old list. The full program also prints that now-empty list and checks that the new snapshot maps back to the same mutable node.

```csharp
using RecursiveAI.OneHtml.Mutation;

var doc = MutableDocument.Parse(
    "<ul><li>Tea</li></ul>" +
    "<ol id='to'></ol>");
var start = doc.Snapshot();
var target = start.Document.GetElementById("to");
var to = start.ToMutable(target);
var item = to.PreviousSibling.FirstChild;
var key = item.Key;
to.AppendChild(item);
doc.Commit();
Console.WriteLine(doc.SerializeNode(to));
Console.WriteLine(key == item.Key);

var after = doc.Snapshot();
Console.WriteLine(doc.SerializeNode(to.PreviousSibling));
Console.WriteLine(after.ToMutable(after.ToNode(item)) == item);
```

```text
<ol id="to"><li>Tea</li></ol>
True
<ul></ul>
True
```

If you try to move a node into its own descendant, `AppendChild` throws `DomException` before changing the tree.

## Variations

**Insert before an existing sibling.** Add this after the full program to put `Tea` before `Coffee`. The reference node must already be a child of `to`.

```csharp
var coffee = doc.CreateElement("li");
coffee.SetTextContent("Coffee");
to.PrependChild(coffee);
to.InsertBefore(item, coffee);
doc.Commit();
Console.WriteLine(doc.SerializeChildren(to));
```

```text
<li>Tea</li><li>Coffee</li>
```

## Related recipes

- [Copy an HTML subtree to another document](https://recursiveai.org/onehtml/cookbook/clone-html-subtree-between-documents/)
- [Replace the contents of an HTML element](https://recursiveai.org/onehtml/cookbook/replace-element-inner-html-with-mutable-dom/)

## The short answer

Call `newParent.AppendChild(node)` when both handles belong to the same `MutableDocument`. If the node already has a parent, onehtml removes it from there and appends it to the end of the new parent’s children. The node’s `Key` stays the same. Commit the edit when you want an immutable snapshot of the result.

## Does appendChild move or copy an existing DOM node?

It moves the node. Like the browser’s [`appendChild`](https://developer.mozilla.org/en-US/docs/Web/API/Node/appendChild), onehtml doesn’t ask you to remove it first. The original list in the program becomes empty, and the destination gets the original `<li>`, not a second element with the same text. A node’s descendants move with it. If you want the original to stay put, use `CloneNode` for a copy in the same mutable document, or `Import` for a copy in a different document.

The node’s position can change without its identity changing. For example, an editor can associate a note with a mutable node and still find it after that node moves. Use `MutableNode` as the dictionary key, or use `Key` in a dictionary scoped to one document. A DOM `id` attribute is just markup; it isn’t the same as the mutable handle or its key.

## How do I insert a DOM node before another child?

Use `InsertBefore` on the new parent when the position matters. Pass the existing node you want to move, then the destination child that should follow it. In the variation, `Coffee` is that reference child. You don’t have to clone `Tea` or work out a numeric child index. For a move to the beginning, `PrependChild` inserts before the current first child.

## How do I find a moved DOM node after a commit?

Keep the `MutableNode` handle and call `ToNode(item)` on the committed snapshot. A snapshot `Node` points to a location in one immutable `Document`, so don’t carry that location over as a numeric index in the next snapshot. The [snapshot mapping](https://recursiveai.org/onehtml/docs/mutable-document/#3-identity-and-the-snapshot-relationship) works both ways: the full program maps the saved handle into the new snapshot, then uses `ToMutable` to recover the same handle. The older snapshot still shows the item in its original list.
