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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Concatenating strings to build form element names

Status
Not open for further replies.

YogeshPancholi

Programmer
Jul 4, 2000
14
0
0
CA
Hi folks. I need your JavaScript skills assistance in helping me solve my problem.

In my HTML form I have several <DIVS> where each DIV contains multiple text input box elements. The DIVs are named idDiv_1, idDiv_2... idDiv_n, while within each DIV the input boxes are named From_1, To_1, SomeText_1, From_2, To_2, SomeText_2,... From_n, To_n, SomeText_n (see below).

<FORM>
<DIV NAME=&quot;idDiv_1>
<INPUT TYPE=&quot;text&quot; NAME=&quot;From_1&quot; onChange=&quot;doFunc(1);&quot;>
<INPUT TYPE=&quot;text&quot; NAME=&quot;To_1&quot;>
<INPUT TYPE=&quot;text&quot; NAME=&quot;SomeText_1&quot;>
... some more <INPUT>
</DIV>
<DIV NAME=&quot;idDiv_2>
<INPUT TYPE=&quot;text&quot; NAME=&quot;From_2&quot; onChange=&quot;doFunc(2);&quot;>
<INPUT TYPE=&quot;text&quot; NAME=&quot;To_2&quot;>
<INPUT TYPE=&quot;text&quot; NAME=&quot;SomeText_2&quot;>
... some more <INPUT>
</DIV>
...
...
<DIV NAME=&quot;idDiv_n>
<INPUT TYPE=&quot;text&quot; NAME=&quot;From_n&quot; onChange=&quot;doFunc(n);&quot;>
<INPUT TYPE=&quot;text&quot; NAME=&quot;To_n&quot;>
<INPUT TYPE=&quot;text&quot; NAME=&quot;SomeText_n&quot;>
... some more <INPUT>
</DIV>
</FORM>


Now here's my problem:
The parameter in doFunc() identifies the DIV number. Now in doFunc(), I concatenate strings that hold the first part of the INPUT box name(ie. &quot;From_&quot;, &quot;To_&quot;, and &quot;SomeText_&quot;) with the function parameter and construct valid element names. I want to extract values from some INPUT boxes, do some data manipulation, then replace the resultant values into other INPUT boxes (are you with me so far...???).

This is where I need your expertise. How can I concatenate the strings, extract values from some form elements, and replace values in others??? My javascript doesn't like code as follows:

doFunc(myParm) {
var myVal
var myResult

var myFrom = &quot;From_&quot;
var myTo = &quot;To_&quot;
var myForm = document.forms[0];
var myStr = &quot;myResult = myForm.&quot; + myFrom + myParm + &quot;.value&quot;

myVal = eval(myStr)
alert(myVal)
}

What am I doing wrong?? Is it possible to construct element names from several component strings, then use the derived names to manipulate elements??

Any assistance will be greatly appreciated.

Many Thanx...


Yogesh Pancholi

&quot;If a pig loses its voice, is it disgruntled?&quot;
 
First, your function needs to have the word function in front of it:

function doFunc(myParm) {
...

Then try:

var myVal = eval(&quot;myForm.&quot; + myFrom + myParm + &quot;.value&quot;)

 
Thank you. That worked fine.....


Yogesh Pancholi

&quot;If a pig loses its voice, is it disgruntled?&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top