sukesh
sukesh

Reputation: 2423

Get Array data into table - jquery

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.

Upvotes: 0

Views: 72

Answers (1)

Regent
Regent

Reputation: 5178

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"/>

Upvotes: 1

Related Questions