---
title: "Insert and remove HTML elements on a live page in C#"
url: https://recursiveai.org/onerender/cookbook/insert-remove-elements-on-live-html-page/
description: "Insert and remove elements on a live HTML page in C# with OneRender. Commit a MutableDocument edit, flush the layout, and save the updated page to PNG."
version: "1.6.1"
---

# Insert and remove HTML elements on a live page in C#

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

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneRender.Image) · [GitHub](https://github.com/recursive-ai-oss/onerender-csharp) · [Docs](https://recursiveai.org/onerender/docs/) · [Download example (.zip)](https://recursiveai.org/onerender/cookbook/insert-remove-elements-on-live-html-page/insert-remove-elements-on-live-html-page.zip)

`Commit()` applies your edits to the document; `Flush()` updates the live page’s layout so your app can repaint it.

```shell
dotnet add package RecursiveAI.OneRender.Image
```

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

`RecursiveAI.OneHtml`, which provides `RecursiveAI.OneHtml.Mutation`, comes with `RecursiveAI.OneRender.Image`.

```csharp
using OneRender;
using OneRender.Image;
using OneRender.Interaction;
using RecursiveAI.OneHtml.Mutation;

var doc = MutableDocument.Parse("""
    <ul id="list"><li>Draft</li></ul>
    """);
var page = InteractivePage.Create(doc,
    new HtmlPageOptions(
        FontProviders.CreateDefault()));
var list = page.MutableNodeOf(
    page.FindElement("list"));
list.FirstChild.Remove();
var item = doc.CreateElement("li");
var label = doc.CreateTextNode("Published");
item.AppendChild(label);
list.AppendChild(item);
doc.Commit();
page.Flush();
var published = page.NodeOf(item);
var text = page.GetTextContent(published);
Console.WriteLine(text);
```

Replaces “Draft” with “Published” on the existing rendered page.

[The example](https://recursiveai.org/onerender/cookbook/insert-remove-elements-on-live-html-page/insert-remove-elements-on-live-html-page.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`.

```text
Published
```

## The full program

```csharp
using OneRender;
using OneRender.Image;
using OneRender.Interaction;
using OneRender.Painting;
using RecursiveAI.OneHtml.Mutation;

var doc = MutableDocument.Parse("""
    <style>li { font: 20px Arial; color: #153b5b }</style>
    <ul id="list"><li>Draft</li></ul>
    """);
var page = InteractivePage.Create(doc,
    new HtmlPageOptions(FontProviders.CreateDefault())
    { ViewportWidth = 240, ViewportHeight = 64 });
var list = page.MutableNodeOf(page.FindElement("list"));
var draft = list.FirstChild;
Save("before.png");
draft.Remove();
var item = doc.CreateElement("li");
item.AppendChild(doc.CreateTextNode("Published"));
list.AppendChild(item);
doc.Commit();
page.Flush();
Console.WriteLine($"Draft attached: {!page.NodeOf(draft).IsNull}");
Console.WriteLine(page.GetTextContent(page.NodeOf(item)));
Save("after.png");

void Save(string path)
{
    var drawing = new DisplayList();
    page.Page.Paint(drawing);
    var image = DisplayListRasterizer.Render(drawing, 240, 64);
    File.WriteAllBytes(path, image.ToPng());
    Console.WriteLine($"Wrote {path}");
}
```

```text
Wrote before.png
Draft attached: False
Published
Wrote after.png
```

Open `before.png` and `after.png`. The bullet reads “Draft” in the first image and “Published” in the second. Both images come from the same `InteractivePage`.

A removed element has no current page node: `page.NodeOf(draft).IsNull` is `true`. Keep the `MutableNode` for later edits, not a snapshot `Node` from before the flush.

## Variations

**Insert before another element.** To keep “Draft” and put “Published” above it, skip `list.FirstChild.Remove()` and replace the `AppendChild` call with:

```csharp
list.InsertBefore(item, list.FirstChild);
```

## Related recipes

- [Change HTML text, classes and attributes in C#](https://recursiveai.org/onerender/cookbook/update-page/)
- [Resize a live HTML viewport and set device pixel ratio in C#](https://recursiveai.org/onerender/cookbook/resize-live-page-and-set-pixel-ratio/)

## The short answer

Parse the HTML into a `MutableDocument` and create the `InteractivePage` from that document, not from the source string. Add or remove children on the mutable tree, call `doc.Commit()` to publish the edits, then `page.Flush()` to update the rendered layout. Paint the current page to make the change visible in your host. A PNG you saved before the flush is still just a snapshot; repaint and save again to see the new list.

## How do I add and remove HTML elements dynamically in C#?

Use `CreateElement` to create an element, `CreateTextNode` for its text, and `AppendChild` to attach it under an existing parent. Creating a node alone doesn’t put it on the page. To remove an item, call `Remove()` on that item, not on its parent. To insert in the middle of a list, use `InsertBefore` on the parent with the existing item as the reference. In the program, the starting list has one child, so `FirstChild` is the element to remove. In a list with line breaks and indentation, that first child could instead be a text node; use a specific element handle when the structure varies.

You can group the remove and insert into one commit, as the example does. If your app is taking a reading between two edits, be aware that a layout query such as `GetBoundingClientRect` on a mutable node commits pending edits before answering. A flush takes the net changes since the last rendered snapshot, so you don’t need a separate repaint for every edit.

## Does removing an HTML element reset the rest of the page?

No. While snapshots can renumber nodes, surviving elements retain their listeners, focus, scroll positions and form state. If you remove the focused element itself, it loses focus. Hold on to `MutableNode` handles and use `page.NodeOf` when a page API needs a current snapshot `Node`, such as when setting a form value. `page.MutableNodeOf` does the reverse, as when the example looks up the list by ID and then edits it. The [mutable document guide](https://recursiveai.org/onerender/docs/game-hosts/#following-a-mutable-document) explains which calls flush automatically and how to release state for nodes you won’t reinsert.
