ARIA Reflection Properties On Elements
ARIA states expose typed reflection properties such as ariaInvalid and ariaLive, replacing setAttribute strings with compiler-checked assignments.
Code
// ARIA states have typed reflection properties on Element: no setAttribute
// strings, and unknown attribute names fail to compile.
interface FieldRefs {
input: HTMLInputElement;
error: HTMLElement;
}
function bindField(inputId: string, errorId: string): FieldRefs | null {
const input = document.getElementById(inputId);
const error = document.getElementById(errorId);
if (!(input instanceof HTMLInputElement) || !error) return null;
return { input, error };
}
function reportProblem(field: FieldRefs, message: string): void {
field.input.ariaInvalid = 'true';
field.error.ariaLive = 'assertive';
field.error.textContent = message;
}
function clearProblem(field: FieldRefs): void {
field.input.ariaInvalid = 'false';
field.error.textContent = '';
}
const field = bindField('email', 'email-error');
if (field) {
reportProblem(field, 'Email is missing an @');
console.log('aria-invalid:', field.input.ariaInvalid);
clearProblem(field);
console.log('aria-invalid:', field.input.ariaInvalid);
}How it works
Everything happens inside the functions bindField, reportProblem and clearProblem.
The result comes back as an object carrying input.
What to watch for
- Only the runtime is needed — no packages to fetch and no installs.
documentexist only in browsers — a Node runtime has no shims for them out of the box.
Using it
The signature doing the main work is function bindField(inputId: string, errorId: string)
The run emits 1 labeled section — aria-invalid: — one per step.
The call returns input.
The key statements, annotated
if (!(input instanceof HTMLInputElement) || !error) return null;
return { input, error };
Interactive Tools for This Topic
WCAG Color Contrast CheckerCalculate WCAG 2.1 color contrast ratios between foreground and background colors. Verify AA and AAA compliance levels for normal and large text accessibility.JSON to Zod Schema ConverterInfer typed Zod z.object schemas from JSON payloads locally, supporting nested objects, merged array shapes, enums, nullable fields, and JSDoc comments.JSON Schema ValidatorValidate JSON data against a JSON Schema definition and get detailed error reports. Supports type checking, required fields, and common constraint validation.JSONL / NDJSON InspectorValidate NDJSON and JSON Lines line-by-line, pinpoint syntax errors by line number, analyze key frequencies and types, and export a pretty JSON array.
FAQ
- Does `bindField` modify its input?
- No. Nothing passed in is written to, which makes the call safe on shared data.
- Does ARIA Reflection Properties On Elements run in plain Node.js?
- No — it touches `document`, which exist only in the browser. Run it in a page (or the DevTools console) instead of a bare Node process.
Report a problem with this exampleOpens a pre-filled issue on GitHub.