dynamically set a field based upon a parameter

Discussion in 'Javascript' started by j_liu21@hotmail.com, Jul 23, 2005.

  1. Guest

    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.
     
    , Jul 23, 2005
    #1
    1. Advertising

  2. cosmic foo Guest

    <> wrote in message
    news:...
    > 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 =
     
    cosmic foo, Jul 24, 2005
    #2
    1. Advertising

  3. web.dev Guest

    Hi J,

    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. :)
     
    web.dev, Jul 24, 2005
    #3
  4. Guest

    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'
    }
     
    , Jul 24, 2005
    #4
  5. Guest

    Brilliant, it worked. Thanks for everyone's help!
     
    , Jul 24, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?QWRpcw==?=
    Replies:
    0
    Views:
    393
    =?Utf-8?B?QWRpcw==?=
    Oct 19, 2004
  2. TJS
    Replies:
    3
    Views:
    571
  3. Reckoner
    Replies:
    1
    Views:
    314
    Diez B. Roggisch
    Nov 5, 2009
  4. William Gower

    Checking a checkbox based upon a table field

    William Gower, May 17, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    155
    Ken Cox [Microsoft MVP]
    May 17, 2004
  5. Scott Gordo
    Replies:
    1
    Views:
    176
    Kyle Peterson
    Mar 23, 2006
Loading...

Share This Page