javascript - Changing variable within document.form1.Q1[n+1].value -


this have far:

for (var n = 0; n < order.length; n++)     {                          (var = 0; < order.length; i++)              {                 if (order[i]==[n+1])                     {                         document.form1.q1[n+1].value = [i+1];                     }             }     };   

this i'm trying achieve, without knowing how long array, or amount of questions in form (though they'll equal each other):

if (order[0] == 1) {  document.form1.q11.value = 1; }  if (order[1] == 1) {  document.form1.q11.value = 2; }  if (order[2] == 1) {  document.form1.q11.value = 3; }  if (order[0] == 2) {  document.form1.q12.value = 1; }  if (order[1] == 2) {  document.form1.q12.value = 2; }  if (order[2] == 2) {  document.form1.q12.value = 3; }  if (order[0] == 3) {  document.form1.q13.value = 1; }  if (order[1] == 3) {  document.form1.q13.value = 2; }  if (order[2] == 3) {  document.form1.q13.value = 3; }   

i'm pretty sure problem inserting variable [n+1] into:

document.form1.q1[n+1].value = [i+1];   

i've tried few different routes can't seem figure out how way, , can't hard code it.

for example:

  (var n = 0; n < order.length; n++)     {                          var q = n+1;         var question = q.tostring();            var string = "q1" + question;              (var = 0; < order.length; i++)                  {                     if (order[i]==[n+1])                         {                             document.form1.['string'].value = [i+1];                         }                 }     }; 

and:

document.forms['form-name']['string'].value = [i+1]; 

at point i've realised i'm in on head.

help hugely appreciated!

thank time :)

that approach string variable not bad, you'll have use variable in property accessor - used string literal 'string' equivalent accessing .string property.

for (var n = 0; n < order.length; n++) {                      var q = n+1;     var question = q.tostring();      var string = "q1" + question;      (var = 0; < order.length; i++) {         if (order[i] == n+1) { // no array literal here!             document.form1[string].value = i+1;         }     } } 

however, there's cleaner approach doesn't need nested loops:

for (var = 0; < order.length; i++) {     var q = order[i];     var string = "q1" + q;     document.forms.form1.elements[string].value = i+1; } 

Comments