Advertisement
gitlez

YA: Simple Javascript Questionaire Pt2

Apr 14th, 2012
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*    Simple Javascript Questionaire    */
  2.  
  3. /*
  4.     In response to a Yahoo Answers Question.
  5.     To be used with: http://pastebin.com/b6U3L4D8
  6. */
  7.  
  8. var questions = [];
  9. questions[0] = {'question': 'What is your Age', 'answers': ['13-18','19-24','25-35','35-50','50+'], 'answer': ''}; // Answer Will Hold the User's Selection
  10. questions[1] = {'question': 'What is your favourite color', 'answers': ['Red', 'Orange', 'Yellow', 'Green', 'Blue', 'Purple'], 'answer': ''};
  11. questions[2] = {'question': 'What is your favourite pet', 'answers': ['Dog','Cat','Rodent','Reptile','Fish'], 'answer': ''};
  12.  
  13.  
  14. var curQ = 0;
  15. var ques = questions[curQ];
  16. function echo(s){ console.log(s); }
  17. function byId(id){
  18.     return document.getElementById(id);
  19. }
  20. function checkForAnswer(){
  21.     var ab = byId('answersBox');
  22.     var inputs = ab.getElementsByTagName('input');
  23.     var answer = '';
  24.     if(inputs.length === 0){ return true; }
  25.     for(x in inputs){
  26.         if(inputs[x].checked){ answer = inputs[x].value; }
  27.     }
  28.     ques.answer = answer;
  29.     return (answer.length > 0);
  30. }
  31. function switchQ(v){
  32.     if( (checkForAnswer() && v > 0) || v < 0 ){
  33.         curQ += v;
  34.         displayQuestion();
  35.     }else {
  36.         alert("Question Must Be Answered Before Proceeding");
  37.     }
  38. }
  39. function disableEnableBtns(){
  40.     byId('prevQBtn').disabled = (curQ === 0);
  41.     byId('nextQBtn').disabled = ( curQ === (questions.length - 1));
  42.     byId('resultsQBtn').disabled = (curQ !== (questions.length - 1));
  43. }
  44. function displayQuestion(){
  45.     if(curQ < 0) { curQ = 0; }
  46.     if(curQ >= questions.length){ curQ = questions.length - 1;}
  47.     disableEnableBtns();
  48.     ques = questions[curQ]; // Current Question Object
  49.     var qb = byId('questionBox'); // Question Box
  50.     var ab = byId('answersBox'); // Answers Box
  51.     qb.innerHTML = ques.question + '?';
  52.     ab.innerHTML = 'Select One of the follow:<br>';
  53.     for( i in ques.answers){
  54.         ab.innerHTML += '<input type="radio" name="theAnswer" value="' + questions[curQ].answers[i] + '">' + questions[curQ].answers[i] + '<br>';
  55.     }
  56.     /*    Check for a current Answer    */
  57.     if(ques.answer.length > 0){
  58.         ab.innerHTML = ab.innerHTML.split('value="' + ques.answer + '"').join('value="' + ques.answer + '" checked="checked"');
  59.     }
  60. }
  61. function displayResults(){
  62.     if(!checkForAnswer()){
  63.         alert('Please answer the last question before seeing the results');
  64.     }else{
  65.         var html = '';
  66.         for( i=0;i<questions.length;++i){
  67.             html += '<span style="font-weight: bold;">' + questions[i].question + '?</span> ' + questions[i].answer + '<br>';
  68.         }
  69.         byId('questionBox').innerHTML = 'Results';
  70.         byId('answersBox').innerHTML = html;
  71.     }
  72.    
  73. }
  74. window.onload = function(){ displayQuestion(); }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement