title: using javascript to loop through asp.net radio button list to get selected value and text
body: <p>I am creating a questions and answers page and the answers are a collection of asp.net radiobuttonlist as per the following: `              </p>
  
  <pre><code>&lt;div class="col-md-4"&gt;
                  &lt;asp:RadioButtonList ID="Q1AnswersRadioButtonList" runat="server" RepeatLayout="Flow"&gt;
                      &lt;asp:ListItem Value="Q1OptionARadioButton"&gt;&lt;/asp:ListItem&gt;
                      &lt;asp:ListItem Value="Q1OptionBRadioButton"&gt;&lt;/asp:ListItem&gt;
                      &lt;asp:ListItem Value="Q1OptionCRadioButton"&gt;&lt;/asp:ListItem&gt;
                      &lt;asp:ListItem Value="Q1OptionDRadioButton"&gt;&lt;/asp:ListItem&gt;
                  &lt;/asp:RadioButtonList&gt;
                  &lt;br /&gt;
                  &amp;nbsp;&lt;textarea class="text-danger" id="Q1CorrectAnswerText" name="Q1CorrectAnswerText" readonly="readonly" wrap="soft"&gt;&lt;/textarea&gt;
              &lt;/div&gt;
  </code></pre>
  
  <p>Now, I am using the following JavaScript function to loop through to find out which one the user selected. The following is the JavaScript code.</p>
  
  <pre><code>function GetUserAnswerOneSelection()
  {
  var elementRef = document.getElementById('Q1AnswersRadioButtonList');
  var radioButtonListArray = elementRef.getElementsByTagName('input');
  var checkedValues = '';
  
  for (var i = 0; i &lt; radioButtonListArray.length; i++) {
      var radioButtonRef = radioButtonListArray[i];
  
      if (radioButtonRef.checked == true) {
          // To get the Text property, use this code:
          var labelArray =   radioButtonRef.parentNode.getElementsByTagName('label');
          if (labelArray.length &gt; 0) {
              if (checkedValues.length &gt; 0)
                  checkedValues += ', ';
              checkedValues += labelArray[i].innerHTML;
          }
      }
  }
  
  userAnswer1 = checkedValues;
  return userAnswer1;
  </code></pre>
  
  <p>}</p>
  
  <p>The problem now is, it always breaks on this line     var radioButtonListArray = elementRef.getElementsByTagName('input');
  and reports that is undefined.
  I need help please.</p>