...

View Full Version : Simple Syntax Question



peterinwa
04-01-2003, 09:12 AM
I am going to have a lot of code like this:

// Function to set item
function setItem(n){
if (n==1){V=parent.image.document.formA.pro1.value}
if (n==2){V=parent.image.document.formA.pro2.value}
if (n==3){V=parent.image.document.formA.pro3.value}
if (n==4)...
}

So I am wondering if I can shorten it by simply having the variable "n" which is handed to the function replace the number digit in "pro"

Something like this:

// Function to set item
function setItem(n){
V=parent.image.document.formA.pro[n].value;
}

I'm sure the answer's really easy or it's not possible. Thanks in advance!

Peter

Roelf
04-01-2003, 09:36 AM
// Function to set item
function setItem(n){
V=parent.image.document.forms["formA"].elements["pro" + n].value;
}

should do the trick

peterinwa
04-01-2003, 09:42 AM
Works great... thank you.

peterinwa
04-01-2003, 06:53 PM
Follow-up question. (I get at least one, don't I?)

In the form the values are pulled from variables with default values, then the user can change the values in the form. The new values are then used by a function and Roelf's code works great.

But I also want to put the values the user entered into the variables to replace the defaults. So now my "simple syntax" question is how do I address a variable, e.g., pro1, pro2, pro3... replacing the numeric digit with "n" as handed to the function.

Something like:

function setVariable(n){
pro[n]=whatever

Thanks again!

Peter

Roelf
04-02-2003, 06:30 AM
why create different variables, and not an array like:
var pro = new Array();
pro[n] = whatever;

then you have all your values together

peterinwa
04-02-2003, 06:53 AM
Great idea. I actually only have 3 now and it's working coded the long way, but I'll do that when I expand.

As to why not... I'm learning!

Thanks again, Peter



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum