dynamically set a field based upon a parameter

J

j_liu21

Is something like this possible?

A form with x fields named
field1, field2, ... fieldx

I'd like to set the field specified in the parameter

function SetUnknownField(FieldID)
{
document.myform.field+FieldID+.value="Something calculated";
}

Any help would be appreciated.
 
C

cosmic foo

Is something like this possible?

A form with x fields named
field1, field2, ... fieldx

I'd like to set the field specified in the parameter

function SetUnknownField(FieldID)
{
document.myform.field+FieldID+.value="Something calculated";
}

Any help would be appreciated.
you have to do something like this,
document.forms.myform.elements["field" + FieldID].value =
 
W

web.dev

Hi J,

Is something like this possible?

A form with x fields named
field1, field2, ... fieldx

I'd like to set the field specified in the parameter

function SetUnknownField(FieldID)
{
document.myform.field+FieldID+.value="Something calculated";
}

Any help would be appreciated.

Yes, you have the right idea. So for example, let's say your form was
like so:

<form action = "uri" method = "post" name = "myForm" id = "myForm">
<input type = "text" name = "field1"/>
<input type = "text" name = "field2"/>
[etc.. fieldx]
</form>

Within your javascript function, you could have the following:

function SetFieldValue(fieldName, fieldValue)
{
document.forms["myForm"].elements[fieldName].value = fieldValue;
}

This way, you can specify any field name and any value that you want to
set it to. If you wanted to modify, you could even pass in the form
name.

Hope this helps. :)
 
J

j_liu21

Awesome thanks web.dev and cosmic foo. One slight twist, I'm using
images and it seems to work in I.E. but not Mozilla, so here is what it
looks like now. Is this valid?

function SetFieldValue(fieldNameID, fieldImage)
{
document.myform.elements["fieldName"+fieldNameID].src =
'images/fieldImage'
}
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top