Welcome to WebmasterWorld Guest from 54.147.225.204

Forum Moderators: open

Message Too Old, No Replies

Cycling through multiple arrays

   
3:41 am on Aug 21, 2012 (gmt 0)

5+ Year Member Top Contributors Of The Month



I need to write a script that cycles through a form to change some values based on one of several templates. I only want to change a given field if the current value is "Unknown".

I'm guessing I will need an array of all the different field ids, and separate arrays for each template with what fields to change, and what they should be changed to. Other than that I'm not sure how to fit them together, and I'm not too familiar with JavaScript array key/value pairs.

var allFields=new Array("id1","id2","id3","id4","id5","id6","id7","id8","id9","id10");
var template1=new Array("id1":"Yes","id4":"No","id5":"Maybe");
var template2=new Array("id2":"Always","id4":"Yes","id9":"Yes","id10":"Never");
var template3=new Array("id5":"Okay");
var template5=new Array("id7":"No","id8":"You Bet");

function useTemplate(templateName){
? ? ?
}
8:15 am on Aug 21, 2012 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



var template1={"id1":"Yes","id4":"No","id5":"Maybe"};
var template2={"id2":"Always","id4":"Yes","id9":"Yes","id10":"Never"};
... // objects NOT arrays

function useTemplate(templateName){
var template = window[templateName];
var id;
for ( id in template ) { document.getElementById(id).value=template[id]; }
}

usage
useTemplate('template1');

or if this usage OK
useTemplate(template1);

function useTemplate(template){
var id;
for ( id in template ) { document.getElementById(id).value=template[id]; }
}
2:55 am on Aug 23, 2012 (gmt 0)

5+ Year Member Top Contributors Of The Month



Thank you very much! That worked very well and was much simpler than I was thinking.