PDA

View Full Version : Does Javascript allow for Adjustable Variables



Brigham
06-20-2008, 07:39 PM
I am finding that this may not be possible with JavaScript, but I thought I would ask. Perhaps someone has another approach that would work just as well.

Background –
I have a ColdFusion script which uses a loop to create multiple variables. The number of variables affects the variable’s name and can vary from use to use. For example if 4 is selected; the loop happens four times, there would be a variable created for Info1, Info2, Info3, and Info4 as well as a Title1, Title2, etc and several other variables with corresponding numbers as well. This part works fine, and it passes to the next script or on to our DB as well.

My dilemma –
I commonly use JavaScript to setup a validation against my ColdFusion. This allows me to do an Alert() popup when a field in a table is unanswered. The trick is in this example I need the variable declared in the JavaScript to adjust according to the number used in the CFLoop. Is that even possible in JS?

Example –

function validateForm()
{
var AlertMe=0;
var ValMessage="";

for (i=1;i<=Qform.count.value;i++)
{
var ValInfo="";
if (Qform.Info.value == "") {var ValInfo="Aditional Information"+i;AlertMe=1;}
if (ValInfo != ""){if (ValMessage != ""){ValMessage += "\n";};ValMessage += ValInfo;}
}

if (AlertMe == 1)
{alert("The following are required fields -\n" + ValMessage + "");return false;}

return true;
}

The actual validation I use contains a lot more checks, but this is the core where my problem lies. Qform.Info.value can I dynamically alter this variable to coincide with the ‘i’ in the For loop?

Any help would be great. An alternate method suggestion would be okay as well.

jscheuer1
06-21-2008, 05:30 AM
If:

Qform

and:

Qform.Info

both exist (preferably as a form and a form element, respectively. But as generic objects would do), then:

Qform.Info.value

may be set to whatever you like.

There can however be only one:

Qform.Info.value

and once it is set to something, it will be that unless and until it is set to something else.

How useful doing that would be would depend upon the rest of your code (including your HTML markup - if it has anything to do with this).

As a side note, if Qform is a form, the more correct way of referring to Qform.Info (if it is an element of the Qform form) would be:

Qform.elements['Info']

But most browsers won't mind if you use:

Qform.Info

Now, if you were to have:

Qform0.Info.value

and:

Qform1.Info.value

These could be separate. Similarly for the Info object(s), if they exist. But value is a generic property of form elements (if that's what we are talking about here), so although you could have value1 and value2, etc. - it wouldn't have the same significance as value visa vi a form element.

Another aspect to all of this is that if you were to have multiple base objects (Qform and/or Qform.Info), these could be set up as instances in some sort of object oriented javascript code. To what end would depend upon just what you are trying to do though.

Brigham
06-23-2008, 02:30 PM
Okay so Qform.Info.value can have only one value at a time, but Qform.Info1.value would be entirely seprate right? So, is there a way in a JS 'For' loop to assign a new number in the variable name (name, not the value) each time through the loop?

something like Qform.Info{i}.value or Qform.Info(i).value or some such.

So in my code above, if Qform.count.value was set to 4 i recieve these variables created.

Qform.Info1.value
Qform.Info2.value
Qform.Info3.value
Qform.Info4.value

or if Qform.elements['Info'] is the proper method i am fine with that as well if it can somehow produce these as it goes through the 'For' loop.

Qform.elements['Info1']
Qform.elements['Info2']
etc. etc.

i take it that an array could do this with Qform.Info[i].value which would give me in a 'For' loop

Qform.Info[1].value
Qform.Info[2].value
etc. etc.

however to use this i would have to completely rewrite my ColdFusion scripts to match the array. Could be done, but would rather not rewrite everything :o

using Qform1.Info.value does not work i don't think since it references that form name (Qform) from my CF script and i do not want 4 seprate forms. well, maybe i could put the loop outside the form (again messy), but even then is it possible to create the effect i need in JS?

Qform1.Info.value
Qform2.Info.value
etc. etc.