get and put value from a dynamicly created textfield

  • Thread starter Pål Sindre Hiåsen
  • Start date
P

Pål Sindre Hiåsen

Hi,

The thing I want to do, is to get - change and put content from text
fields that is dynamically created.
I use PHP to create the form that have textfields in it. The number of
textfields vary.
The name of each field is text2 - text3 - text4 and so on.

So in javascript I want to get the content in text2. And that without
writing:
value = parseInt(document.form1.text3.value);

I want to put this into a for loop and do something like this
for(i=0;i<numfield;i++)
{
value = parseInt(document.form1.["text"+i].value);

}

Is this posible in javascript, and if so what is the syntax?

Regards,

Sindre
 
M

Martin Honnen

Pål Sindre Hiåsen wrote:

I want to put this into a for loop and do something like this
for(i=0;i<numfield;i++)
{
value = parseInt(document.form1.["text"+i].value);


You need to drop that dot that is you need to use
document.form1["text" + i].value
 
P

Pål Sindre Hiåsen

Pål Sindre Hiåsen wrote:

I want to put this into a for loop and do something like this
for(i=0;i<numfield;i++)
{
value = parseInt(document.form1.["text"+i].value);


You need to drop that dot that is you need to use
document.form1["text" + i].value


Thanks!

Sindre
 
T

Thomas 'PointedEars' Lahn

Pål Sindre Hiåsen said:
Pål Sindre Hiåsen said:
I want to put this into a for loop and do something like this
for(i=0;i<numfield;i++)
{
value = parseInt(document.form1.["text"+i].value);


parseInt() by default also parses hexadecimal (with leading `0x') and in
some implementations deprecated octal (with leading zero) values. If you
don't want that, pass a second operand to indicate the target base:

value = parseInt(..., 10);

Parsing any non-decimal value then will result in `NaN' which evaluates
to `false' in a boolean expression.
You need to drop that dot that is you need to use
document.form1["text" + i].value

The both standards compliant and downwards compatible way is

document.forms["form1"].elements["text" + i].value

You should assign overly long and mroe often used references to a variable:

var fe = document.forms["form1"].elements;
if (fe["text" + i])
{
... fe["text" + i].value ...
}

And if you pass the reference to the handling method, referencing becomes
much easier:

....
<head>
...
<meta http-equiv="Content-Script-Type" content="text/javascript">
...
</head>

<body>
...
<form action="..." ...>
<div>
...
<script type="text/javascript">
function handleMe(f)
{
var fe;
if (f && (fe = f.elements))
{
...
if (fe["text" + i])
{
... fe["text" + i].value ...
}
...
}
}

document.write('<input ... onclick="handleMe(this.form);">');
</script>
...
</div>
</form>
...
</body>


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top