Connecting Tech Pros Worldwide Help | Site Map

dynamically set a field based upon a parameter

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 23rd, 2005, 10:55 PM
j_liu21@hotmail.com
Guest
 
Posts: n/a
Default dynamically set a field based upon a parameter

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 23rd, 2005, 11:35 PM
web.dev
Guest
 
Posts: n/a
Default 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 23rd, 2005, 11:35 PM
cosmic foo
Guest
 
Posts: n/a
Default 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, 12:05 AM
j_liu21@hotmail.com
Guest
 
Posts: n/a
Default 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, 12:35 AM
James Andrews
Guest
 
Posts: n/a
Default 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, 01:15 AM
j_liu21@hotmail.com
Guest
 
Posts: n/a
Default Re: dynamically set a field based upon a parameter

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

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.