Execute javascript command from variable

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rmurgia
    New Member
    • May 2008
    • 63

    #1

    Execute javascript command from variable

    There is a sequence of ProdCode fields; in this case 1 - 3. I would like to populate the strCBFilter variable with the values contained in those fields using a loop:
    Code:
    document.frmXRefAttachMaint_01.ProdCode1 
    document.frmXRefAttachMaint_01.ProdCode2 
    document.frmXRefAttachMaint_01.ProdCode3
    The code creates each name by concatenating the number in the loop to the
    prefix: document.frmXRe fAttachMaint_01 .ProdCode". Is there a way to have the program do this:
    Code:
      strCBFilter = strCBFilter  +  document.frmXRefAttachMaint_01.ProdCode1
    using the value of variable strCBFilterCode which contains:
    document.frmXRe fAttachMaint_01 .ProdCode1 with the number changing each time through the loop.

    Full Code:
    Code:
    for(i = 1; i < 4; i++) 
    {
      var strCBFilterCode = "document.frmXRefAttachMaint_01.ProdCode" + i + ".value";
    
       [B]strCBFilter = strCBFilter  +  strCBFilterCode[/B] 
    }
    //The highlighted code populates the strCBFilter variable with the contents of the strCBFilterCode variable instead of the field the strCBFilterCode variable refers to. In other languages, I was able to execute commands after placing them in variables. Can this be done in Javascript?//
    Last edited by gits; Feb 19 '09, 06:11 PM. Reason: added code tags
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    I guess you need to pass your strings through the eval() function to get it executed (otherwise it's just a bunch of strings).

    Comment

    • rmurgia
      New Member
      • May 2008
      • 63

      #3
      It worked! Thank you.

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        I'm glad I could help.

        Comment

        • acoder
          Recognized Expert MVP
          • Nov 2006
          • 16032

          #5
          Instead of eval(), you could use something like:
          Code:
          document.frmXRefAttachMaint_01.elements["ProdCode" + i].value

          Comment

          • rmurgia
            New Member
            • May 2008
            • 63

            #6
            Thank you. Are there advantages of using this code:

            document.frmXRe fAttachMaint_01 .elements["ProdCode" + i].value

            over the eval() function?

            Comment

            • acoder
              Recognized Expert MVP
              • Nov 2006
              • 16032

              #7
              The main benefit here is speed. Using bracket notation (member operators) is much faster - see Note on eval(). You shouldn't get into the habit of bunging everything into eval when there are better alternatives around. You'll notice the benefits when there's more code.

              Comment

              • rmurgia
                New Member
                • May 2008
                • 63

                #8
                Okay, will give it a try and thanks again!

                Comment

                Working...