Connecting Tech Pros Worldwide Forums | Help | Site Map

dynamically set a field based upon a parameter

j_liu21@hotmail.com
Guest
 
Posts: n/a
#1: Jul 23 '05
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.


web.dev
Guest
 
Posts: n/a
#2: Jul 24 '05

re: dynamically set a field based upon a parameter


Hi J,

j_liu21@hotmail.com wrote:[color=blue]
> 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.[/color]

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. :)

cosmic foo
Guest
 
Posts: n/a
#3: Jul 24 '05

re: dynamically set a field based upon a parameter



<j_liu21@hotmail.com> wrote in message
news:1122158155.448935.201150@g14g2000cwa.googlegr oups.com...[color=blue]
> 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.
>[/color]
you have to do something like this,
document.forms.myform.elements["field" + FieldID].value =


j_liu21@hotmail.com
Guest
 
Posts: n/a
#4: Jul 24 '05

re: dynamically set a field based upon a parameter


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'
}

James Andrews
Guest
 
Posts: n/a
#5: Jul 24 '05

re: dynamically set a field based upon a parameter


j_liu21@hotmail.com wrote:[color=blue]
> 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'
> }
>[/color]

Images are not form elements, so they should not be members of the
elements[] array of "myform", which makes things a little confusing that
the above works in IE.

Instead, try accessing them via the images array within document, i.e:

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


James

PS: Try and get in a habit of putting semi-colons at the end of
statements. They are optional in JS, but a missed semi-colon can cause
some very confusing errors which are easily avoided by always using them.
j_liu21@hotmail.com
Guest
 
Posts: n/a
#6: Jul 24 '05

re: dynamically set a field based upon a parameter


Brilliant, it worked. Thanks for everyone's help!

Closed Thread