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

Make control variable -- How?

Status
Not open for further replies.

crvoss

Programmer
May 2, 2005
21
US
I am looking for a method to change several text control object values by doing the following:

var Control = 'myText';
document.myForm.(Control).value = 'wow';

which doesn't work. The following does:

document.myForm.myText.value = 'wow';

but I need to change many controls within a loop. So I am looking for somthing like the first one -- but how dow I make it work?
 
This works:
var Control = 'myText';
var objArray = document.getElementsByName(Control);

The above creates an array of elements that all share the same name, in this case "myText";


But there's not enough information to answer your question.


[monkey][snake] <.
 
Ok, this is more like what I am doing:

Itms = new Array ('2', '3', '1', '7', '1', '3', 'C', 'M', 'Z', 'S', 'S', 'C', 'y', 'P', 'x', 'h', 'a', 'l', 'L', 'W');
NumItms = Itms.length;
var Cntls = new Array ('CuName', 'CuMAddr1', 'CuSAddr1', 'CuMAddr2', 'CuSAddr2', 'CuMCity', 'CuSCity', 'CuMState', 'CuMZip', 'CuSState', 'CuSZip', 'CuMCntry', 'CuSCntry', 'CuPhone1', 'CuMFax1', 'CuPhone2', 'CuFax2', 'CuEmail', 'CustLogo', 'CuWeb');
for(Cnt=0; Cnt<NumItms; Cnt++) {
window.opener.document.fanselect.Cntls[Cnt].value = Itms[Cnt];
}

I am passing a bunch of information from a child window to the parent and placing it in text controls. The array Itms represents the text info being passed. Cntls is an array of control names that the text info is passed to.
 
You can use the eval() command, but I think there's a better way to go about what you're trying to do.

Code:
var Control = 'myText';
eval("document.myForm." + Control + ".value = 'wow'");

[monkey][snake] <.
 
There probably is a better way. Something like your first post using DOM. Maybe assigning an id to each of the controls or something. I'm just not very good with DOM. Your solution worked great, by the way. Thanks.
 
You can also grab all the elements with the (obj).getElementsByTagName("input").

If you have a structure like this:

<div id="buttonDiv">
<input type="button"...... />
<input type="button"...... />
<input type="button"...... />
<input type="button"...... />
<input type="button"...... />
<input type="button"...... />
</div>

you can put all the button objects into an array with this statement:

Code:
var buttonArray = document.getElementById("buttonDiv").getElementsByTagName("input");


[monkey][snake] <.
 
Actually the data being passed to the parent window is a row of data from a table. The table is made up of several records from a database. The user will pick a row to examine. Isn't there a way to id each of the cells in a table? If so your previous post might be real nice for pulling that info from the child window and inserting it in some text input controls in the parent window. I'll have to think on that!

Thanks again for your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top