views:

236

answers:

1

I have a list of radio buttons. Each radio button has a dynamic name. Is there a way to check if they are all selected? Because most radio validation scripts uses a static name.

+1  A: 

If you know the id of some container, you can find the radio buttons with "getElementsByTagName". Thus if your HTML looks something like this:

<form id='x-form' action='...'>
    <input type='radio' name='$[xyz}'>
    <!-- ... -->

then you could check the radio buttons like this:

function allRadioButtonsSelected(formId) {
  var form = document.getElementById(formid);
  var inputs = form.getElementsByTagName('INPUT');
  for (var i = 0; i < inputs.length; ++i) {
    if (inputs[i].type.toLowerCase == 'radio' && !inputs[i].checked)
      return false;
  }
  return true;
}

Your life would be considerably easier if you were using a framework like jQuery.

Pointy
you can use `form.elements` to get a list of all the input elements in a form too.
nickf
true - actually it's been a while since I even thought about doing something like that without jQuery :-)
Pointy