Dynamically assign value to hidden fields?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Brad Melendy

    Dynamically assign value to hidden fields?

    Hello,
    I have a function that among other things, assigns a value to a hidden
    field element. However, I want to pass the name of a hidden field to
    my function and then assign it a value. So instead of this:

    document.forms[0].elements[4].value = "this is a test";

    I want to be able to do something like this:

    document.forms[0].+MyVarName+.va lue = "this is a test";

    Where MyVarName is the name of the field at position 4 in the array of
    form elements.

    Does anyone know if this is possible? When I try to do this I
    generate an error. Thanks for any help!

    ....Brad
  • Vjekoslav Begovic

    #2
    Re: Dynamically assign value to hidden fields?

    You can do that using eval() function, e.g.

    function assignValue(MyV arName){
    str = "document.f orms[0]."+MyVarName+". value = 'this is a test'";
    eval(str);
    }

    but you should consider using DOM, something like this

    document.getEle mentById(MyVarN ame).value = "test"


    "Brad Melendy" <brad@melendy.c om> wrote in message
    news:c58e6818.0 307250844.39482 f3@posting.goog le.com...[color=blue]
    > Hello,
    > I have a function that among other things, assigns a value to a hidden
    > field element. However, I want to pass the name of a hidden field to
    > my function and then assign it a value. So instead of this:
    >
    > document.forms[0].elements[4].value = "this is a test";
    >
    > I want to be able to do something like this:
    >
    > document.forms[0].+MyVarName+.va lue = "this is a test";
    >
    > Where MyVarName is the name of the field at position 4 in the array of
    > form elements.
    >
    > Does anyone know if this is possible? When I try to do this I
    > generate an error. Thanks for any help!
    >
    > ...Brad[/color]


    Comment

    • Lasse Reichstein Nielsen

      #3
      Re: Dynamically assign value to hidden fields?

      "Vjekoslav Begovic" <vjbegovic@inet .hr> writes:
      [color=blue]
      > You can do that using eval() function, e.g.
      >
      > function assignValue(MyV arName){
      > str = "document.f orms[0]."+MyVarName+". value = 'this is a test'";
      > eval(str);
      > }[/color]

      You *never* need eval to access properties. This can be written
      without eval as:
      document.forms[0][MyVarName].value = "this is a test";
      or even better:
      document.forms[0].elements[MyVarName].value = "this is a test";
      [color=blue]
      > but you should consider using DOM, something like this
      >
      > document.getEle mentById(MyVarN ame).value = "test"[/color]

      "document.forms " is just as legal DOM as "document.getEl ementById".

      /L 'please don't top post'
      --
      Lasse Reichstein Nielsen - lrn@hotpop.com
      Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
      'Faith without judgement merely degrades the spirit divine.'

      Comment

      Working...