javascript - Weird behavior on a test im desigining -
this posted has been updated previous post few hours earlier...
i trying make multiple choice test rpg website. 14 questions, , each 1 has 8 answers.
each of 8 answers corresponds 1 of set of 8 variables, upon submission user clicks radio buttons, ends adding points proper variable.
at point comparison run spit out proper results.
the problems had earlier have been solved , have been erased, didnt want start new thread on same topic.
current problem:
i have pieced code: (the full code below)
// go through questions , add +1 each checked // input value = "monk" $('#test').on('click', function() { $('input[name^= "answer"]:checked').each(function(){ if ($('input[name = answer]:checked').val() == "monk"){ secondaryresults.mnk++; console.log(secondaryresults); } })
now above works perfectly, doesnt work if try same exact line of code switiching value , variable++; response adds value first variable.
for example:
question 1 : answer = monk
question 2 : answer = monk
question 3: answer = cleric
question 4: answer = wizard
with code expected console.log spit out
monk:2
cleric:1
wizard:1
instead spits out monk:4
i've tried creating same function different variable (below you'll see second attempt value/var berserker/ber) , attaching same click event. tried putting inside .each() function inside first .on() function. same results each time
so question: need include in above section of code, make code:
- continue count each individual occurence of check 8 variables
- store users score inside object (shown below) later use.
currently code this, 1 input value/variable monk- shortened mnk in object.
all script:
$(window).load(function() { $(".intro").appendto('#display_box'); var question = $(".question"); var questionposition = -1; var secondaryresults = { def:0, ber:0, mnk:0, rng:0, cle:0, thf:0, mge:0, }; var primaryresults = { def:0, ber:0, mnk:0, rng:0, cle:0, thf:0, mge:0, }; function clearbox(){ $("#display_box").children().fadeout(500).appendto('#question_holding'); }; function cycle(){ question.eq(questionposition).fadein(500).appendto("#display_box"); $("#display_box").animate({scrolltop:0}, 500); } $('#leftarrow').on('click', function(){ questionposition--; if (questionposition <= -1) {questionposition = 13}; clearbox(); cycle(); }); $('#rightarrow').on('click', function(){ questionposition++; if (questionposition > 13) { questionposition = 0}; clearbox(); cycle(); if($('input[name^="answer"]:checked').length > 13 ) { $("#submit").css('display', 'block'); } }); $('#test').on('click', function() { $('input[name^= "answer"]:checked').each(function(){ if ($('input[name = answer]:checked').val() == "monk"){ secondaryresults.mnk++; console.log(secondaryresults); } }) }); $('#test').on('click', function() { $('input[name^= "answer"]:checked').each(function(){ if ($('input[name = answer]:checked').val() == "berserker"){ secondaryresults.ber++; console.log(secondaryresults); } }) }); });
Comments
Post a Comment