title: Focusing on Radio Buttons
body: <p><strong>Objective:</strong> Fix the code below, and understand why focusing on radio buttons doesn't work like focusing on text input boxes.</p>
  
  <p><strong>Question:</strong> Why does this not work: </p>
  
  <pre><code>function isValidRadio(elem, helperMsg) {
      var valid = false;
      for (var i = 0; i &lt; elem.length; i++) {
          if (elem[i].checked) {
              return true;
          }
      }
      alert(notice);
      setTimeout("focusElement('" + elem.form.name + "', '" + elem.name + "')", 0);
      return false;
  }
  </code></pre>
  
  <p>...when this does:</p>
  
  <pre><code>function isValidText(elem, notice) {
      var str = elem.value;
      var re = /.+/;
      if(!str.match(re)) {
          alert(notice);         
          setTimeout("focusElement('" + elem.form.name + "', '" + elem.name + "')", 0);
          return false;
      } else {
          return true;
      }
  }
  </code></pre>
  
  <p><em>Example Code:</em></p>
  
  <pre><code>function focusElement(formName, elemName) {
      var elem = document.forms[formName].elements[elemName];
      elem.focus();
      elem.select();
  }
  function validateForm(form) {
      if (isValidText(form.phone, "Fail."))  {
          if (isValidRadio(form.yesNo, "Fail."))  {
              return true;
          }
          return false;
      }
  }
  &lt;form id='myForm' action='/formPage/collectinfo/' method='post' accept-charset='UTF-8' name="myForm"&gt;
      &lt;input name="phone" type="text" maxlength="14" size="14" /&gt;
      &lt;input type="radio" name="yesNo" value="Yes"&gt; Yes &lt;input type="radio" name="yesNo" value="No" &gt; No
  &lt;input type="submit" value=" Submit " name="Submit" onclick="return validateForm(myForm)" /&gt;
  </code></pre>
  
  <p><strong>Details:</strong> I'm using php/javascript/html and so far I've only been testing this piece in Chrome. The form works just fine and the validation works up until it hits isValidRadio. The alert runs, but then the javascript breaks. A code fix would be great, but I wouldn't mind 'fishing for a life time...' </p>