Connecting Tech Pros Worldwide Help | Site Map

dynamically set a field based upon a parameter

  #1  
Old July 23rd, 2005, 11:55 PM
j_liu21@hotmail.com
Guest
 
Posts: n/a
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.

  #2  
Old July 24th, 2005, 12:35 AM
web.dev
Guest
 
Posts: n/a

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

  #3  
Old July 24th, 2005, 12:35 AM
cosmic foo
Guest
 
Posts: n/a

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 =


  #4  
Old July 24th, 2005, 01:05 AM
j_liu21@hotmail.com
Guest
 
Posts: n/a

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

  #5  
Old July 24th, 2005, 01:35 AM
James Andrews
Guest
 
Posts: n/a

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.
  #6  
Old July 24th, 2005, 02:15 AM
j_liu21@hotmail.com
Guest
 
Posts: n/a

re: dynamically set a field based upon a parameter


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

Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 0 November 14th, 2005 04:15 AM
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 0 November 13th, 2005 11:37 PM
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 0 November 13th, 2005 09:56 PM
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 0 November 13th, 2005 03:15 AM