Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > dynamically set a field based upon a parameter

Reply
Thread Tools

dynamically set a field based upon a parameter

 
 
j_liu21@hotmail.com
Guest
Posts: n/a
 
      07-23-2005
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.

 
Reply With Quote
 
 
 
 
cosmic foo
Guest
Posts: n/a
 
      07-23-2005

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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 =


 
Reply With Quote
 
 
 
 
web.dev
Guest
Posts: n/a
 
      07-23-2005
Hi J,

http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> 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.

 
Reply With Quote
 
j_liu21@hotmail.com
Guest
Posts: n/a
 
      07-23-2005
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'
}

 
Reply With Quote
 
j_liu21@hotmail.com
Guest
Posts: n/a
 
      07-24-2005
Brilliant, it worked. Thanks for everyone's help!

 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
javascript validation for a not required field, field is onlyrequired if another field has a value jr Javascript 3 07-08-2010 10:33 AM
set pdb break condition based upon number of hits? Reckoner Python 1 11-05-2009 01:41 AM
1.Enter space bar for field names and save the field.The field shoud not get saved and an alert should be there as"Space bars are not allowed" Sound Javascript 2 09-28-2006 02:43 PM
ASP w/Captcha form field drops last value in field upon submission? Scott Gordo ASP General 1 03-23-2006 06:20 AM
Checking a checkbox based upon a table field William Gower ASP .Net Datagrid Control 1 05-17-2004 07:45 PM



Advertisments