RadioNodeList Reads Checked Radio Groups

Read named radio groups via form.elements and RadioNodeList.value, and clear the group by unchecking each input: assigning '' is not a reset.

JavascriptForm Validation

Code

javascriptsnippet
31 lines
const form = document.querySelector('#shipping');
const group = form.elements.namedItem('speed');

// RadioNodeList.value returns the checked radio's value,
// or '' when nothing is checked yet.
console.log('initial:', JSON.stringify(group.value));

// Setting value through the list checks the matching radio for you.
group.value = 'express';
console.log('after set:', group.value);

// The list is iterable: every input in the group, in DOM order.
for (const radio of group) {
  console.log(radio.value, radio.checked);
}

// The HTMLFormControlsCollection also indexes by name directly.
console.log('same group:', form.elements['speed'] === group);

// Assigning '' is NOT a reset: the value setter only ever checks a radio
// whose value matches, so an empty assignment leaves the choice untouched
// (verified in Chromium: the read-back is still 'express').
group.value = '';
console.log('still selected after assigning "":', JSON.stringify(group.value));

// Reading an untouched group never throws — '' is the 'nothing checked' signal.
// Clearing requires unchecking each input directly:
for (const radio of group) {
  radio.checked = false;
}
console.log('cleared:', JSON.stringify(group.value));

Inside the code

The file’s state is a handful of top-level bindings: form, group.

What to watch for

  • Nothing to vet in a supply-chain review — the file pulls in no packages.
  • The snippet leans on document — browser-only surfaces a bare Node process cannot provide.

Applying it

Look for the initial:, after set:, same group: markers as the output appears; each one traces back to a step.

Statements to notice

const form = document.querySelector('#shipping');
const group = form.elements.namedItem('speed');
group.value = 'express';

Interactive Tools for This Topic

FAQ

Does RadioNodeList Reads Checked Radio Groups 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.