0

An asp:DataList has generated the below html. A Q&A form, where each set has the Qno, Question and options.

 //Repeating Set
<table id="tblQuestions" class="tblQuestions">
 <tr><td><span class="lbQno">1</span><span>First question</span></td></tr>
 <tr>
  <td>
    <table class="clOptions">
     <tr>
      <td><input type="radio" value="1/><label>sometext</label</td>
      <td><input type="radio" value="2/><label>sometext</label</td>
      <td><input type="radio" value="3/><label>sometext</label</td>
     </tr>
    </table>
  </td>
 </tr>
</table>

On a button click, I would like to check that all questions are answered.

JS:

//Get the questionslist
//Loop thro' them, assigning each list to a table.
// and then get the Qno and optionslist in that table

var QuestionsList = document.getElementsByClassName("tblQuestions");
function AllQuestionsAnswered() {

   for(var i = 0;i<QuestionsList.length;i++)
   {
      var tbl = QuestionsList[i];                    
      var OptionsList = $('tbl.clOptions input:radio');

      $('tbl tr').each(function () {
             var QuestionNo = $(this).find('.lbQno').text();
             if(QuestionId > 0){                 
              //perform check on each radiobutton of question
             }                        
      });
   }
}

I am failing here on how to get the controls. All the 3 definitions inside the for loop arent working. How should I proceed further.

4
  • 1
    getElementsByClassName should be getElementById. You are also missing some " and > in your html Commented Sep 11, 2014 at 12:06
  • ID of an element must be unique.. so you can't have the same table structure repeated for each question Commented Sep 11, 2014 at 12:20
  • So it should be class="tblQuestions" in HTML Commented Sep 11, 2014 at 12:20
  • Sorry about that. The table has also the class with the same name. Now updated Commented Sep 11, 2014 at 12:25

1 Answer 1

1

Let us assume that you can correct all problems with HTML:

  • missing " in input's value.
  • missing name for inputs.
  • missing > for </label>.

Then you can use this code for check all necessary questions.

  • Filter all questions that should be checked (based on .lbQno text).
  • For each filtered questions:
    • Get length of selected radio buttons for current question.
    • If there is no selected radio buttons (length equals to 0), then show an error and stop checking.

JavaScript:

$(document).ready(function()
{
    function filterElement()
    {
        return parseInt($(this).find(".lbQno").text()) > 0;
    }

    $('#check').click(function()
    {
        $(".tblQuestions").filter(filterElement).each(function()
        {
            var checkedCount = $(this).find('.clOptions input:radio:checked').length;
            if (!checkedCount)
            {
                alert($(this).find(".lbQno").next().text() + " is not answered");
                return false;
            }
        });
    });
});

Fiddle.

Related HTML:

<table id="tblQuestions1" class="tblQuestions">
 <tr><td><span class="lbQno">1</span><span>First question</span></td></tr>
 <tr>
  <td>
    <table class="clOptions">
     <tr>
      <td><input type="radio" name="q1" value="1"/><label>sometext</label></td>
      <td><input type="radio" name="q1" value="2"/><label>sometext</label></td>
      <td><input type="radio" name="q1" value="3"/><label>sometext</label></td>
     </tr>
    </table>
  </td>
 </tr>
</table>

<table id="tblQuestions2" class="tblQuestions">
 <tr><td><span class="lbQno">1</span><span>Second question</span></td></tr>
 <tr>
  <td>
    <table class="clOptions">
     <tr>
      <td><input type="radio" name="q2" value="1"/><label>sometext</label></td>
      <td><input type="radio" name="q2" value="2"/><label>sometext</label></td>
      <td><input type="radio" name="q2" value="3"/><label>sometext</label></td>
     </tr>
    </table>
  </td>
 </tr>
</table>

<table id="tblQuestions3" class="tblQuestions">
 <tr><td><span class="lbQno">0</span><span>Unnecessary question</span></td></tr>
 <tr>
  <td>
    <table class="clOptions">
     <tr>
      <td><input type="radio" name="q0" value="1"/><label>sometext</label></td>
      <td><input type="radio" name="q0" value="2"/><label>sometext</label></td>
      <td><input type="radio" name="q0" value="3"/><label>sometext</label></td>
     </tr>
    </table>
  </td>
 </tr>
</table>

<input id="check" type="button" value="check"/>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.