d
Amit DhamuSoftware Engineer
 

Check If Radio Button Has Been Checked

1 minute read 00000 views

Say you have a series of radio buttons on one page and for each group (with the same name), you want to ensure that at least one value has been selected. With jQuery you can.

var elements = {}
var count = 0

$(':radio').each(function () {
  elements[$(this).attr('name')] = true
})

$.each(elements, function () {
  count++
})

if ($(':radio:checked').length === count) {
  // All radio groups have a value selected
} else {
  // There is a radio button group with an empty value
}