Click to See Complete Forum and Search --> : Variable - Strings!!!


Sinjorenbloed
06-26-2003, 12:14 PM
Hi!

I'm working on a page and i encountered the following problem:

I have variable that contains a string. And the string-text refers to the content of a textfield in a form. When i print the variable i always get the string-text instead of the value of the textfield to which the string-text in my variable refers. This may sound complicated (or badly explained) so i'll give the code to make it more clear:

in the html-code:

<form name="frmA"
<input type="'text" name="txtB" value="XYZ">
</form>

in the javascript:

tmp="frmA.txtB.Value";
alert(tmp);

=> this prints the text "frmA.txtB.value" instead of the value "XYZ" (instead of the value of the textfield). Is there a way to use the variable 'tmp' as a reference to the content of the textfield and still let it be a string?

PS: when i type:

tmp=frmA.txtB.value;
alert(tmp);

=> in this case i DO get the content of the textfield, but now the variable 'tmp' isn't of the String-type. It's gotta be a string so i can let the string change depending on the input. Can anyone help me?

SlankenOgen
06-26-2003, 12:21 PM
If you are getting the value tmp will be a string.

Besides you can always use tmp = tmp.toString();

~mgb

Sinjorenbloed
06-26-2003, 02:21 PM
Originally posted by SlankenOgen
If you are getting the value tmp will be a string.

Besides you can always use tmp = tmp.toString();

~mgb

Is there any way to get to the textfield the string refers to? (seen as the content of the string is equal to the DOM-path of that textfield)

string contains the text "[form].[textfield].value". (which is the path to the textfield)

David Harrison
06-26-2003, 02:58 PM
erm, shouldn't this:
tmp="frmA.txtB.Value";

be this:
tmp=frmA.txtB.Value;

Sinjorenbloed
06-26-2003, 03:58 PM
Originally posted by lavalamp
erm, shouldn't this:
tmp="frmA.txtB.Value";

be this:
tmp=frmA.txtB.Value;

Normally yes.

but i have multiple textfields so i want to let a counter variate and get something like this:

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

tmp = "form.textfield" + i + ".value";
alert(tmp)
}

But now tmp is a string data type and to be able to do that i gotta know a way to not get the content of the string, but the content of the textfield the string refers to. Or is there another way to do this?

David Harrison
06-26-2003, 04:01 PM
Well in that case couldn't you just use:

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

tmp = form.textfield + i + .value;
alert(tmp)
}

Sinjorenbloed
06-26-2003, 04:05 PM
Originally posted by lavalamp
Well in that case couldn't you just use:

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

tmp = form.textfield + i + .value;
alert(tmp)
}

That's what i thought too. I tried some things like that. They didn't work.

I even tried it with <%=tmp%>. that's how u can use asp-variables in javascript , but it didn't work either. Maybe it's just impossible...

David Harrison
06-26-2003, 04:06 PM
Or, I know that you can refer to images as an array, so I was wondering, could you do the same with textboxes? I haven't tried it and it's pure speculation but it may be worth a try.

jeffmott
06-26-2003, 05:18 PM
but i have multiple textfields so i want to let a counter variate and get something like this:

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

tmp = "form.textfield" + i + ".value";
alert(tmp)
}for (var i = 1; i < 5; ++i)
alert( formName["textfield" + i].value );

Sinjorenbloed
06-27-2003, 01:35 AM
Originally posted by jeffmott
for (var i = 1; i < 5; ++i)
alert( formName["textfield" + i].value );

THANX A LOT!! It works! This is exactly what i was looking for. :D

SlankenOgen
06-27-2003, 06:02 AM
Each form in a document has a length and the elements in each form can be targeted by the array[n][m] notation.

eg.

<html>
<head><title></title>

<script type = "text/javascript">

function a(){
var lgn = document.myForm.length;

for(var i = 0; i < lgn; i++)
{
if(document.myForm[i].type == "text")
{
alert(document.myForm[i].type +":"+document.myForm[i].value)
}
}
}

</script>

</head>

<body onload = "a()">
<form name = "myForm">
<input type = "text" name = "a" value="1">
<input type = "text" name = "b" value="2">
<input type = "text" name = "c" value="3">
<input type = "text" name = "d" value="4">
</form>
</body>
</html>

You could also use

if(document.forms[0][i].type == "text")