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

Help with getting the value from a text field... 1

Status
Not open for further replies.

lerxst3

Programmer
Joined
Feb 24, 2006
Messages
4
Location
US
I need help getting the value from a text box when the name of the text box is in a variable.

So normally, you can simply go:
myval = document.form.textbox.value;

But what I have is:
BoxName = "document.form.textbox.value";

So how do I get the value from the text box using the name that's stored in the variable 'BoxName'?

Thanks!
 
this is why you should never use the implied reference.

always do this:

Code:
var BoxName = document.forms['form'].elements['textbox'].value;



*cLFlaVA
----------------------------
spinning-dollar-sign.gif
headbang.gif
spinning-dollar-sign.gif

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
Sorry, new to JS, so maybe I misunderstood what you said. But based on your post, my code looks like this:

BoxName = document.forms['forms2'].elements['textfield1a1'].value;
myvalue = BoxName;

That didn't work.
 
That's essentially it. Not much more to it than that.

The exact code is this:

for (col = 1; col <= 16; col ++)
{
for (row = 1; row <= cNumRows; row ++)
{
szFieldName = "'textfield" + parseInt(row) + "a" + parseInt(col)+ "'";
BoxName = document.forms['form2'].elements[szFieldName].value;
rgValues[col] = rgValues[col] + parseInt(BoxName);
}
}

However, I did write a small piece of test code with everything hard coded and it still didn't work (which is basically the code snippet I already posted):

boxname = document.forms['form2'].elements['textfield1a1'].value;
myval = boxname;

 
that can't be the exact code, because you reference a variable (cNumRows) that is never declared.

anyway, try this:

Code:
for (col = 1; col <= 16; col ++) {
   for (row = 1; row <= cNumRows; row ++) {
      szFieldName = "textfield" + row + "a" + col;
      BoxName = document.forms['form2'].elements[szFieldName].value;
      rgValues[col] = rgValues[col] + parseInt(BoxName);
   }
}

and what are you doing with rgValues?



*cLFlaVA
----------------------------
spinning-dollar-sign.gif
headbang.gif
spinning-dollar-sign.gif

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
Thanks! That works!

BTW, that was just the relevant code section. cNumRows is declared much earlier.

Thanks again!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top