...

View Full Version : How to => array1[x] = array2 ['word'+x]



Denis
07-07-2002, 08:40 AM
Hello,

I'm a newbie at javascript and trying to populate an array using the following method:

for (x = 1; x < 41; x++){
var y = x+1;
array1[x] = array2 ['word'+y];
}

so has to shorten the code from:

array1[0] = array2 ['term1'];
array1[1] = array2 ['term2'];
array1[2] = array2 ['term3'];
etc etc ....

is this possible in javascript? (my numerous attempt have failed so far)? :(
is ther an equivalent to the php "${"term".$x}"?


this is part of the original working script:

function selectAll() {
var chkBoxes = new Array();
var frm = document.forms['choices1'];
chkBoxes[0] = frm.elements['term1'];
chkBoxes[1] = frm.elements['term2'];
chkBoxes[2] = frm.elements['term3'];
chkBoxes[3] = frm.elements['term4'];
chkBoxes[4] = frm.elements['term5'];
chkBoxes[5] = frm.elements['term6'];
chkBoxes[6] = frm.elements['term7'];
chkBoxes[7] = frm.elements['term8'];
chkBoxes[8] = frm.elements['term9'];
chkBoxes[9] = frm.elements['term10'];
chkBoxes[10] = frm.elements['term11'];
chkBoxes[11] = frm.elements['term12'];
chkBoxes[12] = frm.elements['term13'];
chkBoxes[13] = frm.elements['term14'];
chkBoxes[14] = frm.elements['term15'];
chkBoxes[15] = frm.elements['term16'];
chkBoxes[16] = frm.elements['term17'];
chkBoxes[17] = frm.elements['term18'];
chkBoxes[18] = frm.elements['term19'];
chkBoxes[19] = frm.elements['term20'];
chkBoxes[20] = frm.elements['term21'];
chkBoxes[21] = frm.elements['term22'];
chkBoxes[22] = frm.elements['term23'];
chkBoxes[23] = frm.elements['term24'];
chkBoxes[24] = frm.elements['term25'];
chkBoxes[25] = frm.elements['term26'];
// etc etc...... up to 45
for(var y=0;y<chkBoxes.length;y++) {
chkBoxes[y].checked = true;
}
}

this script, as you surely guessed, automatically "tick" 45 checkboxes by clicking a button inside the "choices1" form.

any help would be greatly appreciated. :)

Denis
07-08-2002, 06:02 AM
Thanks a lot Dave, nice snippet of code that also gives me more insight into javascript. :D



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum