Rinalds Gudriks
Rinalds Gudriks

Reputation: 65

Combine variable with loop

I need to combine 2 variable names, but it must be in a for loop. Also I need to get the variable from a php file to know how long the loop must be.

I have while loop in php file which count how many objects I have and make "$k++" for every object. After while loop I have variable what store count of objects $galak=$k-1 ... I need to make javascript something like:

document.getElementById("galak");
var i;
for (i = 1; i < galak; i++) {
    var decimalMask.i = new InputMask(JST_MASK_DECIMAL, "rdcena".i);
}

After loop "for" there must be decimalMask1,decimalMask2,decimalMask3 etc. and rdcena1,rdcena2,rdcena3 etc.

So question is how to add variable "i" value to other variables decimalMask and rdcena?!

Upvotes: 0

Views: 374

Answers (1)

Sougata Bose
Sougata Bose

Reputation: 31739

Try to concatenate them. Replace . with +. + is the concatenation operator in javascript. And make decimalMask an array.

decimalMask[i] = new InputMask(JST_MASK_DECIMAL, "rdcena" + i);

Then it can be accessed as decimalMask[1] or decimalMask[2]...

Upvotes: 1

Related Questions