Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

array index

Status
Not open for further replies.

cjkenworthy

Programmer
Sep 13, 2002
237
GB
In vbscript I have done arrays like

for i=0 to whatever

response.write(txttermno(i))

next

to output each array value. I want to do something similar in javascript:

for (i = 1; i<=javacount; i++) {

var value = document.frmtermno.txttermno + i + .value;

alert(value)

}

I know that this is the wrong syntax. What is the correct syntax so that I can achieve txttermno(i)?

cheers

 
Try this:
Code:
for (i = 1; i<=document.frmtermno.txttermno.length; i++) {

var value = document.frmtermno.txttermno[i].value;

alert(value)

}

Note: If you've automatically populated your array, the initial index is 0 (zero), not 1, and so you may need to intialize your for loop like this instead:
Code:
for (i = 0; i<document.frmtermno.txttermno.length; i++)
Hope this helps.
Ciao, K----------------
&quot;If you were supposed to understand it, we wouldn't call it code&quot; - FedEx
 
I have a form with an onSubmit=&quot;return checkform(this)&quot;

I have a variable number of fields in the form: txttermno1, txttermno2 etc...

In the <HEAD> i have the function:


function checkform(frmtermno) {

var invalid = &quot; &quot;;
var javacount = document.frmtermno.hdnrecords.value;

for (i = 0; i < javacount; i++) {


if (document.frmtermno.txttermno.value ==&quot;&quot;) {
alert(&quot;You did not enter a Termination Number for number&quot;);
return false;
}

if (document.frmtermno.txttermno.value.indexOf(invalid) > -1) {
alert(&quot;No spaces permitted in a Termination number for number&quot;);
return false;
}

}
}

BUT i get the error: &quot;Object Expected&quot;

Any ideas?
 
In fact I get the error:

&quot;document.frmtermno.txttermno is not an object&quot;


when the form is submitted - any ideas now?
 
Could you post the form (or part of the form) that your working with? Hope this helps.
Ciao, K----------------
&quot;If you were supposed to understand it, we wouldn't call it code&quot; - FedEx
 
its just a standard form with input text boxes named txttermno1, txttermno2 etc...

the boxes are created using an ASP for loop dependant on how many records I have from an ADO recordset.


I'll post it in 2 days when i am back in work.


thanks
 
The Form is called: &quot;termno&quot; and the ASP generates a number of text beoxes dependent on how many records there are:

'in a loop until k = number of records

response.write(&quot;<input type=&quot;&quot;text&quot;&quot; name=&quot;&quot;txttermno&quot; & Trim(k) & &quot;&quot;&quot; maxlength=&quot;&quot;11&quot;&quot; size=&quot;&quot;11&quot;&quot;&quot;)

'loop close

the OnSubmit action is: OnSubmit=&quot;checkform(this)&quot;

so what I need to do is check each txttermno(I) for blanks or spaces:


function checkform(termno) {

var invalid = &quot; &quot;;
var javacount = document.termno.hdnrecords.value;

for (i = 1; i<=javacount; i++) {

if (document.termno.txttermno.value ==&quot;&quot;) {
alert(&quot;You did not enter a Termination Number for number1 &quot;);
return false;
}

if (document.termno.txttermno.value.indexOf(invalid) > -1) {
alert(&quot;No spaces permitted in a Termination number for number1&quot;);
return false;
}

}

}

It doesn't seem to recognise document.termno.txttermno.value as a valid object.

Any Ideas?
 
Going back to your first example:
Code:
for (i = 1; i<=javacount; i++) {
    var value = document.frmtermno.txttermno + i + .value;
    alert(value)
}

Try this instead:

Code:
for (i = 1; i<=javacount; i++) {
    var value = eval(&quot;document.frmtermno.txttermno&quot; + i + &quot;.value&quot;);
    alert(value)
}

The reference to the textbox object needs to be built as a string (in order to add/concatenate the variable number) and then converted (&quot;eval()&quot;) into the object reference.

Kim-
It is easier to square a circle than to get round a mathematician. Augustus de Morgan

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top