javascript - How to insert a string into an array of text inputs? -


basically, have large string splitting, splitting again.

i need use smallest split array place elements text inputs on page.

this javascript

var splitquestions = vals[2].split('\n');                //loop go through current questions               (var = 0; < splitquestions.length - 1; i++)                {                   //trigger question add single question data can added                   $( "#add" ).trigger('click');                    //split current question separate items                   var s = splitquestions[i].split(',');                    //loop go on sections in question                   var count = 0;                   for(var j = 0; j < s.length; j++)                   {                       count = count + 1;                       var qs = document.getelementbyid('questions[' + j +'][' + count + ']').value;                        qs = s[j];                    }                } 

there many questions on page, depending how many user add. each new question block consist of question, 3 wrong answers, , 1 correct answer.

the part going wrong within last loop. need grab each individual element within 's' array, , place within each text input.

this how raw data displayed before split 'splitquestions' variable:

question1,incorrect-answer1,incorrect-answer2,incorrect-answer3,correct-answer question2,incorrect-answer1,incorrect-answer2,incorrect-answer3,correct-answer question3,incorrect-answer1,incorrect-answer2,incorrect-answer3,correct-answer 

as can see above, each question separated line-break, being \n, each individual part comma separated.

each question input has multidimensional variable assigned id. example, using data above, first line of data, along first element (being question1) question[1][1]. example 'incorrect-answer1' on third line of data, question[3][2]. first number question number, , second number element number.

i hope i've explained enough, since little confused on how explain myself since new multidimensional arrays , loops inside loops. please, if need additional information, post comment , i'll best.

if needed, function creates question elements dynamically:

function dynamicform () {      //set counter     var = $('.dynamic-input#form-step2').length + 1;     //alert(i);     //add input     $('a#add').click(function () {         $('<table><tr><td><p><span class="left"><input type="text" class="dynamic-input" name="questions[' +i +'][1]" id="' + + '" placeholder="question" /></span>' + '<span class="right"><input type="text" class="dynamic-input" name="questions[' +i +'][2]" id="' + + '" placeholder="distraction 1" /><br /><input type="text" class="dynamic-input" name="questions[' +i +'][3]" id="' + + '" placeholder="distraction 2" /><br /><input type="text" class="dynamic-input" name="questions[' +i +'][4]" id="' + + '" placeholder="distraction 3" /><br /><input type="text" class="dynamic-input" name="questions[' +i +'][5]" id="' + + '" placeholder="correct answer" /><br /><a href="#">remove</a></span></p></td></tr></table>').fadein("slow").appendto('#extender');         i++;          $("a:contains('remove')").click(function () {                 $(this).parent().parent().remove();         });          return false;     });       //fadeout selected item , remove     $("#form-step2.dynamic-input").on('click', 'a', function () {         $(this).parent().fadeout(300, function () {             $(this).empty();             return false;         });     }); } 

after further discussions op, fixed code ended being what's below. basically, input numbers starting @ index 1 instead of 0, 1 of issues. trying select id while inputs in question had name attribute.

//loop go on sections in question (var j = 1, len = s.length; j <= len; j++) {      $('input[name="questions[' + (i + 1) + '][' + j + ']"]').val(s[j - 1]);  } 

Comments

Popular posts from this blog

assembly - 8086 TASM: Illegal Indexing Mode -

Java, LWJGL, OpenGL 1.1, decoding BufferedImage to Bytebuffer and binding to OpenGL across classes -

javascript - addthis share facebook and google+ url -