Xin
Xin

Reputation: 35

Radio button validation (dynamic name)

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.

Upvotes: 1

Views: 2472

Answers (1)

Pointy
Pointy

Reputation: 413846

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.

Upvotes: 2

Related Questions