homepage Welcome to WebmasterWorld Guest from 54.225.57.156
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
Cycling through multiple arrays
ocon




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

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){
? ? ?
}

 

daveVk




msg:4486698
 8:15 am on Aug 21, 2012 (gmt 0)

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]; }
}

ocon




msg:4487436
 2:55 am on Aug 23, 2012 (gmt 0)

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

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved