javascript - Quiz answer highlighting both options -


i building employability quiz @ university , each question has yes , no answer.

if click yes answer, highlights green , shows answer below.

if click no answer however, highlights red, , highlights yes answer box green though hasn't been clicked.

this question 1 html:

    $(document).ready(function() {        //get total of questions        var $questionnumber = $('h2').length;        console.log($questionnumber);        //caching final score        var $totalscore = 0;          $('li').click(function() {          //caching variables          var $parent = $(this).parent();          var $span = $(this).find('.fa');            //deactivate options on click          $parent.find('li').off("click");            //check .correct class          //if yes          if ($(this).hasclass('correct')) {            //add .correctanswer class            $(this).addclass('correctanswer');            //find next span , change icon            $span.removeclass('fa fa-square-o').addclass('fa fa-check-square-o');            //reduce opacity of siblings            $(this).siblings().addclass('fade');            //show answer            var $answerreveal = $parent.next('.answerreveal').show();            var $toshowcorrect = $answerreveal.find('.quizzanswerc');            var $toshowfalse = $answerreveal.find('.quizzanswerf');            $toshowcorrect.show();            $toshowfalse.remove();            //add 1 total score            $totalscore += 1;            //console.log($totalscore);          } else {            //add .wronganswer class            $(this).addclass('wronganswer').addclass('fade');            //change icon            $span.removeclass('fa fa-square-o').addclass('fa fa-check-square-o');            //reduce opacity of siblings            $(this).siblings().addclass('fade');            //show wrong message            var $answerreveal = $parent.next('.answerreveal').show();            var $toshowcorrect = $answerreveal.find('.quizzanswerc');            var $toshowfalse = $answerreveal.find('.quizzanswerf');            $toshowcorrect.remove();            $toshowfalse.show();            //locate correct answer , highlight            $parent.find('.correct').addclass('correctanswer');          };        }); //end click function          //print results        function printresult() {          var resulttext = '<p>';          if ($totalscore === $questionnumber) {            resulttext += 'you got ' + $totalscore + ' out of ' + $questionnumber + '! </p>';            $('.resultcontainer').append(resulttext);            $('#halftext').append('<p>wow, one!</p>');            $('#halfimage').append('<img src="http://placehold.it/350x150" width="100%"><img>');          } else if ($totalscore >= 3 && $totalscore < $questionnumber) {            resulttext += 'you got ' + $totalscore + ' out of ' + $questionnumber + '! </p>';            $('.resultcontainer').append(resulttext);            $('#halftext').append('<p>thats pretty good, there still more do.</p>');            $('#halfimage').append('<img src="http://placehold.it/350x150" width="100%"><img>');          } else if ($totalscore < 3) {            resulttext += 'you got ' + $totalscore + ' out of ' + $questionnumber + ' </p>';            $('.resultcontainer').append(resulttext);            $('#halftext').append('<p>dont worry, careers team here on track!</p>');            $('#halfimage').append('<img src="http://placehold.it/350x150" width="100%"><img>');          }          }; //end function          //final score        $('ul').last().click(function() {          //prevent further clicks on          $(this).off('click');          //show result after last li clicked          var $height = $('.finalresult').height();          printresult();          $('.finalresult').show();          $('html, body').animate({              scrolltop: $(document).height() - $height            },            1400);        });        }); //end dom ready
body {    background: #fff;    font-family: 'open sans', sans-serif;    font-size: 1em;  }  .container {    width: 100%;  }  .inner {    width: 60%;    margin: 0 auto;  }  ul {    list-style-type: none;    margin-left: -40px;  }  h2 {    margin-top: 50px;  }  /*********************** list ***********************/    .simplelistanswer:hover {    /*background:#fff195;*/    cursor: pointer;  }  .simplelistanswer,  .quizzanswer {    width: 100%;    background: #f2f2f2;    padding: 9px;    margin-top: 12px;    border: 1px solid #d8d8d8;  }  .simplelisttext {    margin-left: 8px;    font-size: 19px;    color: #3d3d3d;  }  /***************************answer reveal******************/    .quizzanswerc,  .quizzanswerf,  .finalresult {    background: #fefefe;    border: 1px solid #ddd;    width: 100%;  }  .answerreveal {    display: none;    width: 100%;  }  .answerheader div {    color: #84f272;    margin-top: 10px;  }  #bravo,  #sorry {    width: 100%;    margin-left: 10px;    margin-top: 15px;    margin-right: 10px;    font-size: 15px;  }  .answerheader {    margin-left: 20px;    width: 100%;  }  h3.correctmessage {    color: #88f078;    font-size: 30px;  }  h3.wrongmessage {    color: #ff1f1f;    font-size: 30px;  }  .fa.fa-check-circle,  .fa.fa-times-circle {    padding-right: 10px;  }  .correctanswer {    background: #88f078;  }  .wronganswer {    background: #ff1f1f;  }  .fade {    opacity: .6;    cursor: default;  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>  <h2>1. know where/how find out work experience, internship , placement opportunities</h2>  <ul class="simplelist">    <li class="simplelistanswer correct">      <span class="fa fa-square-o"></span>      <span class="simplelisttext">yes know look.</span>    </li>      <li class="simplelistanswer">      <span class="fa fa-square-o"></span>      <span class="simplelisttext">no i'm not sure.</span>    </li>  </ul>  <!--end simplelist-->    <div class="answerreveal">      <div class="quizzanswerc">      <div class="answerheader">        <h3 class="correctmessage"><i class="fa fa-check-circle "></i>awesome!</h3>      </div>      <!--end answer header-->      <div class="answertext">        <p id="bravo">well done!</p>      </div>      <!--end asnwertext-->    </div>    <!--end quizzanswerc-->      <div class="quizzanswerf">      <div class="answerheader">        <!--<h3 class="wrongmessage"><i class="fa fa-times-circle"></i>oops!</h3>-->      </div>      <!--end answer header-->      <div class="answertext">        <p id="sorry">you can find more information on in room 014</p>      </div>      <!--end asnwertext-->    </div>    <!--end quizzanswerf-->  </div>  <!--end answerreveal-->

any assistance in stopping double-selection happening appreciated can't work out why it's happening!

you should remove line:

$parent.find('.correct').addclass('correctanswer'); 

from else section of jquery. it's selecting correct answer , highlighting -- shouldn't.

jsfiddle example


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -