---
title: "Select an HTML radio button programmatically in C#"
url: https://recursiveai.org/onerender/cookbook/set-radio-button-selection-in-rendered-html/
description: "Use OneRender to select a radio button in rendered HTML, clear the previous selection in the same group, and read the current checked state without a click."
version: "1.4.0"
---

# Select an HTML radio button programmatically 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/set-radio-button-selection-in-rendered-html/set-radio-button-selection-in-rendered-html.zip)

Use `SetChecked` to select a radio button without a click on a page that OneRender renders itself, with no browser. The previous choice in the group clears.

```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.

```csharp
using OneRender;
using OneRender.Image;
using OneRender.Interaction;

const string html = """
    <input type=radio name=plan id=basic
           checked>
    <input type=radio name=plan id=plus>
    """;
var options = new HtmlPageOptions(
    FontProviders.CreateDefault());
var page = InteractivePage
    .Create(html, options);
var document = page.Page.Document;
var basic = document
    .GetElementById("basic");
var plus = document
    .GetElementById("plus");
page.SetChecked(plus, true);
Console.WriteLine(
    $"Basic: {page.IsChecked(basic)}");
Console.WriteLine(
    $"Plus: {page.IsChecked(plus)}");
```

[The example](https://recursiveai.org/onerender/cookbook/set-radio-button-selection-in-rendered-html/set-radio-button-selection-in-rendered-html.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 selection prints as:

```text
Basic: False
Plus: True
```

## The full program

```csharp
using OneRender;
using OneRender.Image;
using OneRender.Interaction;

const string html = """
    <input type=radio name=plan id=basic checked>
    <input type=radio name=plan id=plus>
    """;
var options = new HtmlPageOptions(FontProviders.CreateDefault());
var page = InteractivePage.Create(html, options);
var document = page.Page.Document;
var basic = document.GetElementById("basic");
var plus = document.GetElementById("plus");
int events = 0;
foreach (var radio in new[] { basic, plus })
    foreach (var type in new[] { "click", "input", "change" })
        page.Events.AddEventListener(EventTarget.Of(radio), type, _ => events++);

Console.WriteLine($"Basic before: {page.IsChecked(basic)}");
page.SetChecked(plus, true);
Console.WriteLine($"Basic: {page.IsChecked(basic)}");
Console.WriteLine($"Plus: {page.IsChecked(plus)}");
Console.WriteLine($"Click, input or change events: {events}");
```

`dotnet run` prints:

```text
Basic before: True
Basic: False
Plus: True
Click, input or change events: 0
```

The full program listens for `click`, `input` and `change` on both radios. The zero count is expected: `SetChecked` changes form state without dispatching events. Pass an `<input type=radio>` from the page; passing a non-checkable node returns `InputOutcome.None` and doesn’t change the page.

## Variations

**Clear the selected option.** Add this after selecting `plus` to leave the group with no selection:

```csharp
page.SetChecked(plus, false);
Console.WriteLine(page.IsChecked(plus));
```

## Related recipes

- [Edit text in a rendered HTML form](https://recursiveai.org/onerender/cookbook/edit-text-in-rendered-form/)
- [Read the keyboard tab order](https://recursiveai.org/onerender/cookbook/inspect-html-keyboard-tab-order/)
- [Handle HTML pointer events](https://recursiveai.org/onerender/cookbook/hit-testing-events/)

---

## The short answer

Call `page.SetChecked(radio, true)` on the radio input in a OneRender `InteractivePage`. It checks that option and unchecks any previously checked option in the group. Read the live value with `page.IsChecked`, and use the same `InteractivePage` when you render or inspect the form again. You don’t need to change the source markup or send a pointer event.

## How do I select a radio button in HTML from C#?

Use `checked` in the HTML for the initial choice. After creating the page, use `SetChecked` to restore a preference or select an option in response to another part of your app. The sample deliberately leaves `checked` on Basic in the input string. After the change, `IsChecked(basic)` is false: looking for that attribute in the document would still tell you what the initial markup said, not what the control shows now. OneRender also uses the live state when styling the `:checked` selector.

## How do you group radio buttons in HTML?

Give the choices the same nonempty `name` and the same [form owner](https://html.spec.whatwg.org/multipage/input.html#radio-button-group). The example’s two inputs are both outside a form, so sharing `name=plan` is enough. Their IDs are different so the host can look up each one. Matching names in different forms don’t make one group. If selecting an option leaves another one checked, inspect the names and which form owns each control.

`SetChecked` is for changing state, not emulating a user action. If your app has a handler that normally recalculates a total in response to `change`, call that calculation from the code that restores the selection too; the state change alone won’t invoke the handler.
