I am just doing some exercise with JQuery. I want to create a dynamic questionnaire with JQuery and XML. Here I want to keep my all questions in a XML file. My application should able to select random questions from the XML file and create html content with Question and its answers with radio buttons. Once user submits his response he should able to see the result and Explanation for each question.
HTML Page
- <html>
- <head>
- <title> Creating questionnaire with JQuery and XML </title>
- <script src="http://code.jquery.com/jquery-1.11.2.min.js"></script>
- <script type="text/javascript" src="readXML.js"></script>
- <style>
- .mydiv {
- display:table-cell;
- }
- .mydiv.table-hidden{
- display:none;
- }
- table, th , td
- {
- border: 1px solid grey;
- border-collapse: collapse;
- padding: 5px;
- }
- table tr:nth-child(odd)
- {
- background-color: #f1f1f1;
- }
- table tr:nth-child(even)
- {
- background-color: #ffffff;
- }
- </style>
- </head>
- <body>
- <h2>Creating questionnaire with JQuery and XML</h2> <hr>
- <div id="ContentArea"></div>
- <br><button id="submit">SUBMIT</button>
- </body>
- </html>
ReadXML.js-
- $(document).ready(function()
- {
- $(document).on('click','#submit',function(e)
- { $(this).hide();
- submit();
- });
-
- var Show = 5;
- var array = [];
-
-
- $.get("questionBank.xml",{},function(xml)
- { for (i = 0; i < Show; )
- {
- var newVal=Math.ceil(Math.random() * $(xml).find("Question").length );
- if ($.inArray(newVal, array) < 0)
- {
- i++;
- array.push(newVal)
- }
-
- }
- myQuestionnaireHTMLOutput = '';
- myQuestionnaireHTMLOutput += '<table width="98%" border="1" cellpadding="0" cellspacing="0" id="tblQuestion">';
- var SrNo=1;
-
- myQuestion ='';
-
-
- $('Question',xml).each(function(i)
- {
- QuestionID = $(this).find("QuestionID").text();
-
- for (i = 0; i < Show; i++)
- {
- if(QuestionID ==array[i])
- {
- if($.inArray(QuestionID, array) < 0)
- {
- Question = $(this).find("QuestionText").text();
- QuestionID = $(this).find("QuestionID").text();
- QuestionType = $(this).find("QuestionType").text();
- QuestionExplanation = $(this).find("QuestionExplanation").text();
- options=$(this).find("options");
- strOptions ='<div class="myDivClass">';
- $('option', options).each(function (obj)
- {
- istrue=$(this).attr("istrue");
- strOptions +='<input type="radio" data-istrue='+istrue+' name='+QuestionID+' value='+$(this).text()+'>'+$(this).text()+'<br>';
- });
-
-
- myQuestion = BuildQuestionHTML(SrNo++,Question,QuestionID,strOptions+'</desc>',QuestionExplanation);
-
- }
- myQuestionnaireHTMLOutput = myQuestionnaireHTMLOutput + myQuestion ;
- myQuestion ='';
- break;
- }
- }
- });
-
- myQuestionnaireHTMLOutput += '</table>';
-
-
- $("#ContentArea").append(myQuestionnaireHTMLOutput );
-
- });
- });
-
- function BuildQuestionHTML(SrNo,Question,QuestionID,options,QuestionExplanation)
- {
- output = '';
- output += '<tr><td width="2%">'+SrNo+'</td>';
- output += '<td>'+ Question+'<br>';
- output += options+'</td><td class="mydiv table-hidden"><div >'+QuestionExplanation+'</div></td>';
- output += '</tr>';
- return output;
- }
-
- function submit()
- {
- $(".mydiv").toggleClass('table-hidden');
- var collection = $(".myDivClass");
- collection.each(function()
- {
- $(this.parentNode.parentNode).css( "background", "" );
- var QuestionID=$(this).find(':input:first').attr("name");
- if($('input[name="'+QuestionID+'"]:checked').data('istrue')==1 )
- {
- $(this.parentNode.parentNode).css( "background", "#c8ebcc" );
- }
- else
- {
- $(this.parentNode.parentNode).css( "background", "red" );
- }
- });
- }
XML Structure - <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
- <questionBank>
- <Question>
- <QuestionText>2+5=?</QuestionText>
- <QuestionID>1</QuestionID>
- <QuestionType>Single</QuestionType>
- <options>
- <option istrue='1'>7</option>
- <option>8</option>
- <option>9</option>
- <option>10</option>
- </options>
- <QuestionExplanation>Explanation 1</QuestionExplanation>
- </Question>
- </questionBank>