Question on creating variables on the fly

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tom Moroow

    #1

    Question on creating variables on the fly

    Hi, I'm pretty new to javascript and was wondering how you would piece
    together a variable name and then assign it a value. I want to create a
    hidden field and assign it a value based on the value of another variable

    Right now it looks like:

    ("document.all. SM_MARK_10" + dateNumber + ".value")

    where dateNumber is an already defined integer. What I want is to say

    document.all.SM _MARK_1001.valu e="XX"
    document.all.SM _MARK_1002.valu e="X2"

    etc.

    based on that value of dateNumber. Any way to do this easily?




  • Lee

    #2
    Re: Question on creating variables on the fly

    Tom Moroow said:[color=blue]
    >
    >Hi, I'm pretty new to javascript and was wondering how you would piece
    >together a variable name and then assign it a value. I want to create a
    >hidden field and assign it a value based on the value of another variable
    >
    >Right now it looks like:
    >
    >("document.all .SM_MARK_10" + dateNumber + ".value")
    >
    >where dateNumber is an already defined integer. What I want is to say
    >
    >document.all.S M_MARK_1001.val ue="XX"
    >document.all.S M_MARK_1002.val ue="X2"
    >
    >etc.
    >
    >based on that value of dateNumber. Any way to do this easily?[/color]

    It sounds like there must be a better way to do whatever it is
    that you're actually trying to accomplish.

    One solution might be to have two hidden fields already in the
    form, named "VARNAME" and "VARVALUE", and assign your computed
    name to the first and the value to the second. Use names that
    are more meaningful in your application, though.

    It would help to know what you want to do. Your terminology is
    off a bit, since form fields are not the same as variables.

    You should also find a new reference manual that doesn't teach
    you to use "document.a ll", since that is out of date and never
    worked on all browsers.

    Comment

    • Brian

      #3
      Re: Question on creating variables on the fly


      "Tom Moroow" <trow@socal.rr. com> wrote in message
      news:PvlCb.3786 3$NC5.25223@new ssvr29.news.pro digy.com...[color=blue]
      > Hi, I'm pretty new to javascript and was wondering how you would piece
      > together a variable name and then assign it a value. I want to create a
      > hidden field and assign it a value based on the value of another variable
      >
      > Right now it looks like:
      >
      > ("document.all. SM_MARK_10" + dateNumber + ".value")
      >
      > where dateNumber is an already defined integer. What I want is to say
      >
      > document.all.SM _MARK_1001.valu e="XX"
      > document.all.SM _MARK_1002.valu e="X2"
      >
      > etc.
      >
      > based on that value of dateNumber. Any way to do this easily?
      >
      >
      >
      >[/color]

      Use eval(string)



      Comment

      • BigRed

        #4
        Re: Question on creating variables on the fly

        "Tom Moroow" <trow@socal.rr. com> wrote in message news:<PvlCb.378 63$NC5.25223@ne wssvr29.news.pr odigy.com>...

        [color=blue]
        > Hi, I'm pretty new to javascript and was wondering how you would piece
        > together a variable name and then assign it a value. I want to create a
        > hidden field and assign it a value based on the value of another variable
        >
        > Right now it looks like:
        >
        > ("document.all. SM_MARK_10" + dateNumber + ".value")
        >
        > where dateNumber is an already defined integer. What I want is to say
        >
        > document.all.SM _MARK_1001.valu e="XX"
        > document.all.SM _MARK_1002.valu e="X2"
        >
        > etc.
        >
        > based on that value of dateNumber. Any way to do this easily?[/color]


        Here's how you piece together an Element's ID with out hardcoding it.
        Make sure you use the element's ID rather then the NAME attribute.


        <input type="button" ID="myID" value="Hello"
        onClick="findVa lue('ID')">

        <script>
        function findValue(strEl ementID)
        {
        //Find and Read the value
        alert(document. getElementById( 'my'+strElement ID).value);

        //Find and Change the value
        document.getEle mentById('my'+s trElementID).va lue = "Goodbye";
        }
        </script>

        Comment

        • Richard Cornford

          #5
          Re: Question on creating variables on the fly

          "Brian" <BrianGenisio@n ospam.yahoo.com > wrote in message
          news:3fd9f238@1 0.10.0.241...
          <snip>[color=blue][color=green]
          >>("document.al l.SM_MARK_10" + dateNumber + ".value")
          >>
          >>where dateNumber is an already defined integer. What I want is to say
          >>
          >>document.all. SM_MARK_1001.va lue="XX"
          >>document.all. SM_MARK_1002.va lue="X2"[/color][/color]
          <snip>[color=blue]
          >Use eval(string)[/color]

          The *tiny* sub-set of JavaScript programming in which the use of eval is
          appropriate:-

          <URL: http://www.jibbering.com/faq/#FAQ4_40 >

          - does not ever include dynamic property accessor construction as that
          is facilitated (both more reliably and more efficiently) with the
          standard bracket notation property accessor syntax:-

          <URL: http://www.jibbering.com/faq/#FAQ4_39 >

          Richard.


          Comment

          • Thomas 'PointedEars' Lahn

            #6
            Re: Question on creating variables on the fly

            Richard Cornford wrote:
            [color=blue]
            > The *tiny* sub-set of JavaScript programming in which the use of
            > eval is appropriate [...] does not ever include dynamic property
            > accessor construction as that is facilitated (both more reliably
            > and more efficiently) with the standard bracket notation property[/color]
            ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^[color=blue]
            > accessor syntax:-[/color]
            ^^^^^^^^^^^^^^^
            Or shorter: The member operator[1] `[...]' :)
            [color=blue]
            > <URL: http://www.jibbering.com/faq/#FAQ4_39 >[/color]




            PointedEars
            ___________
            [1] Previously falsely addressed by me as the `index operator'.
            --
            The last words of an *n*x admin:
            # fsck /dev/hda8
            /dev/hda8 ist already mounted. Destroy directory structure anyway?<y> yes
            Please stand by...

            Comment

            • Richard Cornford

              #7
              Re: Question on creating variables on the fly

              "Thomas 'PointedEars' Lahn" <PointedEars@we b.de> wrote in message
              news:3FDBA67B.6 010709@PointedE ars.de...
              <snip>[color=blue][color=green]
              >>... the standard bracket notation property[/color]
              > ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^[color=green]
              >> accessor syntax:-[/color]
              > ^^^^^^^^^^^^^^^
              >Or shorter: The member operator[1] `[...]' :)[/color]

              That is one term that could be used but ECMA 262 section 11.2.1 is
              headed "Property Accessors" and I would rather use the terminology from
              the standard.

              Richard.


              Comment

              • Chris

                #8
                Re: Question on creating variables on the fly

                Tom,

                You may need to look at the eval() function.
                Here's a short example

                <html
                <script>
                dateNumber = "01";
                eval("SM_MARK_1 0"+dateNumber+" ='XX'");
                alert(SM_MARK_1 001); // Displays XX
                </script>
                </html>

                You can use it to reference named fields in your document in the sort of way
                you mentioned as in the following example:

                <html>

                <body>
                <input id="SM_MARK_100 1" value=6></input>
                </body>

                <script>
                dateNumber = "01";
                newValue = 28;
                eval("document. all.SM_MARK_10" +dateNumber+".v alue="+newValue );
                </script>

                </html>

                Health warning. Use this knowledge with caution as it becomes very easy to
                write WORN code (Write Once Read Never).

                Regards,
                Chris.

                "Tom Moroow" <trow@socal.rr. com> wrote in message
                news:PvlCb.3786 3$NC5.25223@new ssvr29.news.pro digy.com...[color=blue]
                > Hi, I'm pretty new to javascript and was wondering how you would piece
                > together a variable name and then assign it a value. I want to create a
                > hidden field and assign it a value based on the value of another variable
                >
                > Right now it looks like:
                >
                > ("document.all. SM_MARK_10" + dateNumber + ".value")
                >
                > where dateNumber is an already defined integer. What I want is to say
                >
                > document.all.SM _MARK_1001.valu e="XX"
                > document.all.SM _MARK_1002.valu e="X2"
                >
                > etc.
                >
                > based on that value of dateNumber. Any way to do this easily?
                >
                >
                >[/color]


                Comment

                • Lasse Reichstein Nielsen

                  #9
                  Re: Question on creating variables on the fly

                  "Chris" <c_bartlett@btc onnect.com> writes:

                  Please don't top post!
                  [color=blue]
                  > You may need to look at the eval() function.[/color]

                  .... but most likely not.

                  <URL:http://jibbering.com/faq/#FAQ4_40> ("When should I use eval?")
                  [color=blue]
                  > <script>[/color]

                  (the type attribute is required in HTML)
                  [color=blue]
                  > dateNumber = "01";
                  > eval("SM_MARK_1 0"+dateNumber+" ='XX'");[/color]

                  Or:
                  this["SM_MARK_10"+da teNumber]='XX';

                  (since this script is running in the global scope, "this" refers to
                  the global object which holds the global variables as properties)
                  [color=blue]
                  > alert(SM_MARK_1 001); // Displays XX[/color]
                  [color=blue]
                  > You can use it to reference named fields in your document in the sort of way
                  > you mentioned as in the following example:[/color]

                  By "named field", I assume you mean elements with an id?
                  [color=blue]
                  > <input id="SM_MARK_100 1" value=6></input>
                  > </body>
                  >
                  > <script>[/color]

                  (scripts are not valid outside of the head and body tags. Put it
                  before </body>)
                  [color=blue]
                  > dateNumber = "01";
                  > newValue = 28;
                  > eval("document. all.SM_MARK_10" +dateNumber+".v alue="+newValue );[/color]

                  The document.all collection is a proprietary Microsoft invention. The
                  W3C version is the document.getEle mentById function.

                  In either case, eval is *definitly* not needed. Us either:

                  document.all["SM_MARK_10"+da teNumber].value = newValue;

                  or:

                  document.getEle mentById("SM_MA RK_10"+dateNumb er).value = newValue;

                  (also consider how your version would work if newValue="text" -
                  because it wouldn't).

                  [color=blue]
                  > Health warning. Use this knowledge with caution as it becomes very easy to
                  > write WORN code (Write Once Read Never).[/color]

                  Eval should be avoided, except by experienced programmers who know
                  when to use it. In far the most cases, it is not needed, and there
                  are better (safer, more efficient, more readable) ways.

                  /L
                  --
                  Lasse Reichstein Nielsen - lrn@hotpop.com
                  DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
                  'Faith without judgement merely degrades the spirit divine.'

                  Comment

                  • Lee

                    #10
                    Re: Question on creating variables on the fly

                    Chris said:[color=blue]
                    >
                    >Tom,
                    >
                    >You may need to look at the eval() function.
                    >Here's a short example
                    >
                    ><html
                    ><script>
                    > dateNumber = "01";
                    > eval("SM_MARK_1 0"+dateNumber+" ='XX'");
                    > alert(SM_MARK_1 001); // Displays XX
                    ></script>
                    ></html>
                    >
                    >You can use it to reference named fields in your document in the sort of way
                    >you mentioned as in the following example:
                    >
                    ><html>
                    >
                    ><body>
                    > <input id="SM_MARK_100 1" value=6></input>
                    ></body>
                    >
                    ><script>
                    > dateNumber = "01";
                    > newValue = 28;
                    > eval("document. all.SM_MARK_10" +dateNumber+".v alue="+newValue );
                    ></script>
                    >
                    ></html>
                    >
                    >Health warning. Use this knowledge with caution as it becomes very easy to
                    >write WORN code (Write Once Read Never).[/color]

                    In fact, it should absolutely never be used in that way.
                    Please read the FAQ.


                    Comment

                    • Chris

                      #11
                      Re: Question on creating variables on the fly

                      Lee,
                      I stand corrected.

                      Tom,
                      To be FAQ compliant I think I should have suggested:
                      document.getEle mentById("SM_MA RK_10"+dateNumb er).value=newVa lue;
                      instead of eval() for document elements.

                      I'm not sure there's a more respectable alternative for variables (example
                      1 - the subject of your question).
                      Maybe Lee can assist here.

                      Regards,
                      Chris.


                      "Lee" <REM0VElbspamtr ap@cox.net> wrote in message
                      news:brqark01el 0@drn.newsguy.c om...[color=blue]
                      > Chris said:[color=green]
                      > >
                      > >Tom,
                      > >
                      > >You may need to look at the eval() function.
                      > >Here's a short example
                      > >
                      > ><html
                      > ><script>
                      > > dateNumber = "01";
                      > > eval("SM_MARK_1 0"+dateNumber+" ='XX'");
                      > > alert(SM_MARK_1 001); // Displays XX
                      > ></script>
                      > ></html>
                      > >
                      > >You can use it to reference named fields in your document in the sort of[/color][/color]
                      way[color=blue][color=green]
                      > >you mentioned as in the following example:
                      > >
                      > ><html>
                      > >
                      > ><body>
                      > > <input id="SM_MARK_100 1" value=6></input>
                      > ></body>
                      > >
                      > ><script>
                      > > dateNumber = "01";
                      > > newValue = 28;
                      > > eval("document. all.SM_MARK_10" +dateNumber+".v alue="+newValue );
                      > ></script>
                      > >
                      > ></html>
                      > >
                      > >Health warning. Use this knowledge with caution as it becomes very easy[/color][/color]
                      to[color=blue][color=green]
                      > >write WORN code (Write Once Read Never).[/color]
                      >
                      > In fact, it should absolutely never be used in that way.
                      > Please read the FAQ.
                      > http://www.jibbering.com/faq
                      >[/color]


                      Comment

                      • Lee

                        #12
                        Re: Question on creating variables on the fly

                        Chris said:[color=blue]
                        >
                        >Lee,
                        >I stand corrected.
                        >
                        >Tom,
                        >To be FAQ compliant I think I should have suggested:
                        > document.getEle mentById("SM_MA RK_10"+dateNumb er).value=newVa lue;
                        >instead of eval() for document elements.
                        >
                        >I'm not sure there's a more respectable alternative for variables (example
                        >1 - the subject of your question).
                        >Maybe Lee can assist here.[/color]

                        window["SM_MARK_10"+da teNumber]

                        But if you're having to create variables on the fly, you probably
                        need to rethink the design. You should probably be using arrays
                        or objects.

                        Comment

                        • Thomas 'PointedEars' Lahn

                          #13
                          Re: Question on creating variables on the fly

                          Lee wrote:
                          [color=blue]
                          > window["SM_MARK_10"+da teNumber][/color]

                          Provided that the underlying markup was

                          <body>
                          <input id="SM_MARK_100 1" value=6></input>
                          </body>

                          this will work only with the IE browser component, implementing members
                          of the document.all collection as properties of the `window' object.


                          PointedEars

                          Comment

                          • Richard Cornford

                            #14
                            Re: Question on creating variables on the fly

                            "Thomas 'PointedEars' Lahn" <PointedEars@we b.de> wrote in message
                            news:brsem6$6rs r7$1@ID-107532.news.uni-berlin.de...[color=blue][color=green]
                            >> window["SM_MARK_10"+da teNumber][/color]
                            >
                            >Provided that the underlying markup was
                            >
                            > <body>
                            > <input id="SM_MARK_100 1" value=6></input>
                            > </body>
                            >
                            >this will work only with the IE browser component,
                            >implementing members of the document.all collection
                            >as properties of the `window' object.[/color]

                            No, Chris's challenge relates to creating JavaScript variables on the
                            fly and Lee's answer exploits the fact that JavaScript global variables
                            are also properties of the global (window, on web browsers) object.
                            Lee's response is intended to be a non-eval alternative for -
                            eval("SM_MARK_1 0"+dateNumber+" ='XX'"); - as a method for creating a
                            global variable and in the context of its use:-

                            window["SM_MARK_10"+da teNumber] = 'XX';

                            -is an equally effective and more efficient alternative.

                            Richard.


                            Comment

                            • Grant Wagner

                              #15
                              Re: Question on creating variables on the fly

                              Richard Cornford wrote:
                              [color=blue]
                              > "Thomas 'PointedEars' Lahn" <PointedEars@we b.de> wrote in message
                              > news:brsem6$6rs r7$1@ID-107532.news.uni-berlin.de...[color=green][color=darkred]
                              > >> window["SM_MARK_10"+da teNumber][/color]
                              > >
                              > >Provided that the underlying markup was
                              > >
                              > > <body>
                              > > <input id="SM_MARK_100 1" value=6></input>
                              > > </body>
                              > >
                              > >this will work only with the IE browser component,
                              > >implementing members of the document.all collection
                              > >as properties of the `window' object.[/color]
                              >
                              > No, Chris's challenge relates to creating JavaScript variables on the
                              > fly and Lee's answer exploits the fact that JavaScript global variables
                              > are also properties of the global (window, on web browsers) object.
                              > Lee's response is intended to be a non-eval alternative for -
                              > eval("SM_MARK_1 0"+dateNumber+" ='XX'"); - as a method for creating a
                              > global variable and in the context of its use:-
                              >
                              > window["SM_MARK_10"+da teNumber] = 'XX';
                              >
                              > -is an equally effective and more efficient alternative.
                              >
                              > Richard.[/color]

                              I'm starting to use this:

                              // outside of any function or code block
                              var global = this;
                              // anywhere in the script
                              global["SM_MARK_10 " + dateNumber] = 'XX';

                              As per your suggestion I believe.

                              --
                              | Grant Wagner <gwagner@agrico reunited.com>

                              * Client-side Javascript and Netscape 4 DOM Reference available at:
                              *


                              * Internet Explorer DOM Reference available at:
                              *
                              http://msdn.microsoft.com/workshop/a...ence_entry.asp

                              * Netscape 6/7 DOM Reference available at:
                              * http://www.mozilla.org/docs/dom/domref/
                              * Tips for upgrading JavaScript for Netscape 7 / Mozilla
                              * http://www.mozilla.org/docs/web-deve...upgrade_2.html


                              Comment

                              Working...