Dynamic object names

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DogBot
    New Member
    • Sep 2007
    • 11

    Dynamic object names

    I know so little about javascript that I can't even articlulate the question properly. So, instead here is what I am trying to do.

    [CODE=javascript]
    var i = document.myform .myselectfield. selectedIndex;
    var sel = document.myform .myselectfield. options[i].text;
    var i=0
    while (i<=9)
    {
    eval("document. xform" + i + ".custom.value" ) = sel;
    i=i+1
    }[/CODE]

    The problem is with line 6. I am trying to dynamically assign a value to a field in a series of forms named xform1, xfrom2....xform 9
    Last edited by DogBot; Sep 19 '07, 11:01 PM. Reason: to clarify
  • dmjpro
    Top Contributor
    • Jan 2007
    • 2476

    #2
    Originally posted by DogBot
    I know so little about javascript that I can't even articlulate the question properly. So, instead here is what I am trying to do.

    [CODE=javascript]
    var i = document.myform .myselectfield. selectedIndex;
    var sel = document.myform .myselectfield. options[i].text;
    var i=0
    while (i<=9)
    {
    eval("document. xform" + i + ".custom.value" ) = sel;
    i=i+1
    }[/CODE]

    The problem is with line 6. I am trying to dynamically assign a value to a field in a series of forms named xform1, xfrom2....xform 9

    Welcome to TSDN.
    [code=javascript]
    eval("document. xform" + i + ".custom.va lue= "+sel);
    //try this
    [/code]

    Best of Luck.

    Kind regards,
    Dmjpro.

    Comment

    • acoder
      Recognized Expert MVP
      • Nov 2006
      • 16032

      #3
      eval is evil! Avoid wherever possible.

      Try:[CODE=javascript]document.forms["xform"+i].custom.value = sel;[/CODE]

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5390

        #4
        yep ... don't use eval ... there is no need to use eval besides to evaluate a json-string or something like that ... that might be the result of an ajax-request for example ...

        kind regards

        Comment

        • DogBot
          New Member
          • Sep 2007
          • 11

          #5
          Learned a lot with just this question!!!

          Eval in all languages is evil, but it is always good to know how it works,

          The first response answers my question exactly as I posted it and shows me how to properly use eval..thank you.

          I will be using the non-eval sample however as you all recommend.

          Thank you all very much.

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            Glad to hear you got it working. If you have any more JavaScript problems, you know where to come.

            Comment

            Working...