arrays - How To Loop Through Radio Questions Back Button Javascript -


i'm trying loop through array of objects 'back' , 'next' buttons. reason doesn't loop properly. when 'next' button pressed, 'back' button should decrement how every many times more next button pressed.

this function accessing button go through sets of radio questions. see full code please visit js fiddle. thanks!

     var questions = {      allquestions: [           {             topquestion: [" 1a) click on music producer, produced justins timberlake 4th album?", "2a)click on famous celebrity did justin timberlake date in 1999? ", "3a)click on social media movie did justin timberlake starred in?", "4a)click on famous disney kids show did first justin timberlake made first appearance?", "5a)click on famous singer did justin timberlake accidently tear clothes off during performance?", "6a)what magazine named justin timberlake stylish man in america?"],             question: "1a)what popular site did justin timberlake invest 2.2 million dollars in? ",             choices: ["linkedin", "facebook", "myspace", "youtube"],          }, {             question: "2b)select movie did justin timberlake film score in 2008?",             choices: ["the incredibles", "shark tank", "finding memo", "star wars"],             correctanswer: 1         }, {              question: "3b)what city justin timberlake born in?",             choices: ["chicago", "detroit", "tenessee", "new york"],             correctanswer: 2         }, {   ] };        var newques = object.create(questions);           var k = 0;          var chngeradio = 0;            (var = 0; < 4; i++) {       container = document.getelementbyid("container");     list = document.getelementbyid("list");     var li = document.createelement("input");     li.type = 'radio';     li.name = 'radio_group';     li.id = 'id1';     li.value = newques.allquestions[i].correctanswer;     list.style.textalign = "center";      document.body.appendchild(li);     div = document.createelement("div");     text = document.createtextnode(newques.allquestions[0].choices[i]);     list.appendchild(div);     div.appendchild(li);     div.appendchild(text);  }        btn1.onclick = function (event) {         event = eventutil.getevent(event);           k++;          while (list.firstchild) {             list.removechild(list.firstchild);         };           (var m = 0; m < 4; m++) {               container = document.getelementbyid("container");             list = document.getelementbyid("list");             var li = document.createelement("input");             li.type = 'radio';             li.name = 'radio_group';             li.id = 'id1';             li.value = newques.allquestions[m].correctanswer;             list.style.textalign = "center";             div = document.createelement("div");             text = document.createtextnode(newques.allquestions[k].choices[m])             //alert(k);             list.appendchild(div);             div.appendchild(li);             div.appendchild(text);          };              // assigns event object button,           // go , loop radio questions reason decrements 1 time     if ( k >= 1) {          btn2.onclick = function(event){             event = eventutil.getevent(event);              chngeradio++;                   function replacenode() {               function replacenode() {               if (k === 1) {                  //assigns count 0                  chngeradio -= 1;               };                if (k === 2) {                  //assigns count 1                  chngeradio = 2;                  chngeradio -= 1;              };            };            replacenode()               while (list.firstchild) {                 list.removechild(list.firstchild);             };                (var d = 0; d < 4; d++) {                   container = document.getelementbyid("container");                 list = document.getelementbyid("list");                 var li2 = document.createelement("input");                 li2.type = 'radio';                 li2.name = 'radio_group';                 li2.id = 'id2';                 li2.value = newques.allquestions[d].correctanswer;                 li2.style.textalign = "center";                 div2 = document.createelement("div");                 text2 = document.createtextnode(newques.allquestions[chngeradio].choices[d])                 //alert(k);                 list.appendchild(div2);                 div2.appendchild(li2);                 div2.appendchild(text2);              };          };      };       }; 

i'd suggest not use chngeradio , replacenode()function , replace k--. also, change if condition on back btn.click k>1.

if ( k > 1) {     btn2.onclick = function (event) {         event = eventutil.getevent(event);         k--;          while (list.firstchild) {             list.removechild(list.firstchild);         };          (var d = 0; d < 4; d++) {             container = document.getelementbyid("container");             list = document.getelementbyid("list"); 

...

        text2 = document.createtextnode(newques.allquestions[k].choices[d]);         //alert(k);         list.appendchild(div2);         div2.appendchild(li2);         div2.appendchild(text2);     }; }; 

Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -