setSelectValue
Selects the options a value stands for.
Selects the options a value stands for. The compiler calls it for <select value={...}>. A
single value behaves like select.value = value (null and undefined are the empty string).
On a <select multiple> an array selects exactly the options whose value it lists and clears
the others. The server writes the same state as selected on the options (ssrSelectValue).
Import#
ts
import { setSelectValue } from "@shadow-js/web";
Signature#
ts
function setSelectValue(select: HTMLSelectElement, value: unknown): void
Parameters#
| Name | Description |
|---|---|
select |
The select element; its options must already be in it. |
value |
A string or number (written as String(value)), or null/undefined (written as "": an option with an empty value is selected, otherwise none). For a multiple select, an array of them: null and undefined entries are skipped. An array on a single select is written as String(array). |
Examples#
ts
import { setSelectValue } from "@shadow-js/web";
const select = document.createElement("select");
select.multiple = true;
select.append(new Option("A", "a"), new Option("B", "b"), new Option("C", "c"));
setSelectValue(select, ["a", "c"]); // A and C selected, B cleared