Is it possible to create dynamic var

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

    Is it possible to create dynamic var


    For instance,

    function addNew(i) {
    var 'element'&i
    ...
    }

    If possible, how to?

    Thanks.
  • Laser Lips

    #2
    Re: Is it possible to create dynamic var

    You can use the eval function....

    var somVar ="SomeValue" ;

    var Elementname = "var yourName" + somVar + "='Hello';" ;

    eval(Elementnam e);
    alert(yourNameS omeValue);

    Graham

    Comment

    • Thomas 'PointedEars' Lahn

      #3
      Re: Is it possible to create dynamic var

      DL wrote:
      For instance,
      >
      function addNew(i) {
      var 'element'&i
      ..
      }
      >
      If possible, how to?
      While eval() is a possibility, and a bad one at that, chances
      are that you really don't need what you want. Consider this:

      function addNew(i)
      {
      var a = [];
      a[i] = 42;
      ...
      }


      PointedEars
      --
      Anyone who slaps a 'this page is best viewed with Browser X' label on
      a Web page appears to be yearning for the bad old days, before the Web,
      when you had very little chance of reading a document written on another
      computer, another word processor, or another network. -- Tim Berners-Lee

      Comment

      • Laser Lips

        #4
        Re: Is it possible to create dynamic var

        On May 10, 3:34 pm, Thomas 'PointedEars' Lahn <PointedE...@we b.de>
        wrote:
        DL wrote:
        For instance,
        >
        function addNew(i) {
        var 'element'&i
        ..
        }
        >
        If possible, how to?
        >
        While eval() is a possibility, and a bad one at that, chances
        are that you really don't need what you want.  Consider this:
        >
          function addNew(i)
          {
            var a = [];
            a[i] = 42;
            ...
          }
        >
        PointedEars
        --
        Anyone who slaps a 'this page is best viewed with Browser X' label on
        a Web page appears to be yearning for the bad old days, before the Web,
        when you had very little chance of reading a document written on another
        computer, another word processor, or another network. -- Tim Berners-Lee
        What alternatives are there?

        Comment

        • DL

          #5
          Re: Is it possible to create dynamic var

          On May 10, 10:34 am, Thomas 'PointedEars' Lahn <PointedE...@we b.de>
          wrote:
          DL wrote:
          For instance,
          >
          function addNew(i) {
          var 'element'&i
          ..
          }
          >
          If possible, how to?
          >
          While eval() is a possibility, and a bad one at that, chances
          are that you really don't need what you want.  Consider this:
          >
            function addNew(i)
            {
              var a = [];
              a[i] = 42;
              ...
            }
          >
          Thank you both. I kind of like the above approach, it does not seem
          to solve the problem.
          Here's more detail:

          function addNew(i) {
          var newF = document.getEle mentById('tbl') ;
          var a = [];
          a[i] = newF.insertRow( );
          // ...
          }

          The above code failed. A related question, how to increment a value?
          The following won't work
          // the i value is param for a function like the above one, not be
          concerned
          var rowCount = 1;
          rowCount = eval(rowCount + i);
          alert (rowCount);

          Comment

          • VK

            #6
            Re: Is it possible to create dynamic var

            On May 10, 7:53 am, DL <tatata9...@gma il.comwrote:
            For instance,
            >
            function addNew(i) {
            var 'element'&i
            ..
            >
            }
            >
            If possible, how to?
            Javascript is not a prehistoric BASIC subset for such perversions. If
            you need uniformly accessible elements of unknown in advance amount
            then use array:

            var myElements = new Array;

            function addNew(i) {
            myElements[i] = whatever;
            }

            If you really need properties named like "foo1', "foo2" etc. then use
            object's squared brackets notation:

            var myElements = new Object;

            function addNew(i) {
            myElements['foo'+i] = whatever;
            }

            Comment

            • Dr J R Stockton

              #7
              Re: Is it possible to create dynamic var

              In comp.lang.javas cript message <876f5ddc-6d98-4995-b0d0-8c8e110522e5@s5
              0g2000hsb.googl egroups.com>, Sat, 10 May 2008 07:31:42, Laser Lips
              <loudsphiers@gm ail.composted:
              >You can use the eval function....
              >
              >var somVar ="SomeValue" ;
              >
              >var Elementname = "var yourName" + somVar + "='Hello';" ;
              >
              >eval(Elementna me);
              >alert(yourName SomeValue);
              If you had read the newsgroup FAQ carefully, and its Notes, you would
              have known better than to suggest that.

              It's a good idea to read the newsgroup c.l.j and its FAQ. See below.

              --
              (c) John Stockton, nr London UK. ?@merlyn.demon. co.uk IE7 FF2 Op9 Sf3
              news:comp.lang. javascript FAQ <URL:http://www.jibbering.c om/faq/index.html>.
              <URL:http://www.merlyn.demo n.co.uk/js-index.htmjscr maths, dates, sources.
              <URL:http://www.merlyn.demo n.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.

              Comment

              • DL

                #8
                Re: Is it possible to create dynamic var

                On May 10, 6:45 pm, VK <schools_r...@y ahoo.comwrote:
                On May 10, 7:53 am, DL <tatata9...@gma il.comwrote:
                >
                For instance,
                >
                function addNew(i) {
                var 'element'&i
                ..
                >
                }
                >
                If possible, how to?
                >
                Javascript is not a prehistoric BASIC subset for such perversions. If
                you need uniformly accessible elements of unknown in advance amount
                then use array:
                >
                var myElements = new Array;
                >
                function addNew(i) {
                 myElements[i] = whatever;
                >
                }
                >
                If you really need properties named like "foo1', "foo2" etc. then use
                object's squared brackets notation:
                >
                var myElements = new Object;
                >
                function addNew(i) {
                 myElements['foo'+i] = whatever;
                >
                >
                >
                }- Hide quoted text -
                >
                - Show quoted text -
                Thanks. First let me get simple thing off the list first, your

                var myElements = new Array;
                is probably similar to
                var myElements = []; // the later is a short form probably, yes?

                Now, back to the main topic. Also, I should have been clearer about
                the intent of the code, that is, to dynamically add TR or (TRs) for an
                existing table with ID of 'tbl'.

                Neither Array nor Object works for this case while
                by doing so by hand, e.g.
                var newF = document.getEle mentById('tbl') ;
                var tr1 = newF.insertRow( );

                would work. But

                var newF = document.getEle mentById('tbl') ;
                var myElements = new Array;

                function addNew(i) {
                myElements[i] = newF.insertRow( );

                won't work.

                It seems to be me pretty odd that javascript can't handle 'dynamic
                variable assignment'

                Thanks.

                Comment

                • DL

                  #9
                  Re: Is it possible to create dynamic var

                  On May 10, 10:06 pm, DL <tatata9...@gma il.comwrote:
                  On May 10, 6:45 pm, VK <schools_r...@y ahoo.comwrote:
                  >
                  >
                  >
                  >
                  >
                  On May 10, 7:53 am, DL <tatata9...@gma il.comwrote:
                  >
                  For instance,
                  >
                  function addNew(i) {
                  var 'element'&i
                  ..
                  >
                  }
                  >
                  If possible, how to?
                  >
                  Javascript is not a prehistoric BASIC subset for such perversions. If
                  you need uniformly accessible elements of unknown in advance amount
                  then use array:
                  >
                  var myElements = new Array;
                  >
                  function addNew(i) {
                   myElements[i] = whatever;
                  >
                  }
                  >
                  If you really need properties named like "foo1', "foo2" etc. then use
                  object's squared brackets notation:
                  >
                  var myElements = new Object;
                  >
                  function addNew(i) {
                   myElements['foo'+i] = whatever;
                  >
                  }- Hide quoted text -
                  >
                  - Show quoted text -
                  >
                  Thanks.  First let me get simple thing off the list first, your
                  >
                  var myElements = new Array;
                  is probably similar to
                  var myElements = []; // the later is a short form probably, yes?
                  >
                  Now, back to the main topic.  Also, I should have been clearer about
                  the intent of the code, that is, to dynamically add TR or (TRs) for an
                  existing table with ID of 'tbl'.
                  >
                  Neither Array nor Object works for this case while
                  by doing so by hand, e.g.
                  var newF = document.getEle mentById('tbl') ;
                  var tr1 = newF.insertRow( );
                  >
                  would work. But
                  >
                  var newF = document.getEle mentById('tbl') ;
                  var myElements = new Array;
                  >
                   function addNew(i) {
                   myElements[i] = newF.insertRow( );
                  >
                  won't work.
                  >
                  It seems to be me pretty odd that javascript can't handle 'dynamic
                  variable assignment'
                  >
                  Thanks.- Hide quoted text -
                  >
                  - Show quoted text -
                  Ok, you guys are right, I don't even need dynamic vars. Now got a
                  minor question,
                  the following attempt of setting a newly created cell alignment to
                  right won't work,
                  e.g. newCell1.style. align = "right";
                  what's wrong?

                  Thanks.

                  Comment

                  • Andrew Poulos

                    #10
                    Re: Is it possible to create dynamic var

                    DL wrote:
                    Ok, you guys are right, I don't even need dynamic vars. Now got a
                    minor question,
                    the following attempt of setting a newly created cell alignment to
                    right won't work,
                    e.g. newCell1.style. align = "right";
                    what's wrong?
                    >
                    Shouldn't it be
                    newCell1.style. textAlign = "right";

                    Andrew Poulos

                    Comment

                    • VK

                      #11
                      Re: Is it possible to create dynamic var

                      On May 11, 7:32 am, DL <tatata9...@gma il.comwrote:
                      e.g. newCell1.style. align = "right";
                      align="left/right/center" is an attribute of the cell itself, not a
                      CSS rule.
                      Either:
                      newCell.align = 'right';
                      or
                      newCell.style.t extAlign = 'right';
                      // the latter is lesser functional than the first one



                      Comment

                      • Thomas 'PointedEars' Lahn

                        #12
                        Re: Is it possible to create dynamic var

                        DL wrote:
                        On May 10, 10:34 am, Thomas 'PointedEars' Lahn <PointedE...@we b.de>
                        wrote:
                        >DL wrote:
                        >>For instance,
                        >>function addNew(i) {
                        >>var 'element'&i
                        >>..
                        >>}
                        >>If possible, how to?
                        >While eval() is a possibility, and a bad one at that, chances
                        >are that you really don't need what you want. Consider this:
                        >>
                        > function addNew(i)
                        > {
                        > var a = [];
                        > a[i] = 42;
                        > ...
                        > }
                        >
                        Thank you both. I kind of like the above approach, it does not seem
                        to solve the problem.
                        The problem is that you are apparently resistant to studying for yourself.
                        You will not achieve anything until you solve this problem.
                        Here's more detail:
                        >
                        function addNew(i) {
                        var newF = document.getEle mentById('tbl') ;
                        var a = [];
                        a[i] = newF.insertRow( );
                        // ...
                        }
                        >
                        The above code failed.
                        A useless error description. See also <http://jibbering.com/faq/#FAQ4_43>.

                        And that it failed is unlikely, since it is syntactically correct since
                        JavaScript 1.3 (NN 4), JScript 2.0 (NT 4), ECMAScript Ed. 3. It is more
                        likely that you don't know what you are doing, and therefore you have used
                        it wrong.

                        What replaces the ellipsis matters here. You may have expected `a[1]' to be
                        accessed as `a1' later which is not the case. My solution provides you with
                        a way to let go of several numbered variables and to use an array data
                        structure instead.
                        A related question, how to increment a value?
                        The following won't work
                        See above.
                        // the i value is param for a function like the above one, not be
                        concerned
                        var rowCount = 1;
                        rowCount = eval(rowCount + i);
                        RTFM. The above *equals* the most simple

                        rowCount = rowCount + i;

                        or

                        rowCount += i;

                        That is, provided `i' is a number value. If it is not, you will observe NaN
                        as result or string concatenation instead (e.g. rowCount === "14" if i ===
                        "4"). To convert a value to the number type explicitly, there are several ways.

                        RTFFAQ: http://jibbering.com/faq/#FAQ4_21
                        alert (rowCount);
                        Should be

                        window.alert(ro wCount);


                        PointedEars
                        --
                        var bugRiddenCrashP ronePieceOfJunk = (
                        navigator.userA gent.indexOf('M SIE 5') != -1
                        && navigator.userA gent.indexOf('M ac') != -1
                        ) // Plone, register_functi on.js:16

                        Comment

                        • Thomas 'PointedEars' Lahn

                          #13
                          Re: Is it possible to create dynamic var

                          VK wrote:
                          On May 11, 7:32 am, DL <tatata9...@gma il.comwrote:
                          >e.g. newCell1.style. align = "right";
                          >
                          align="left/right/center" is an attribute of the cell itself, not a
                          CSS rule.
                          Obviously you don't know what a CSS rule is:


                          Either:
                          newCell.align = 'right';
                          or
                          newCell.style.t extAlign = 'right';
                          // the latter is lesser functional than the first one
                          ^^^^^^^^^^^^^^^ ^^
                          Considering we will celebrate CSS2's 10th birthday tomorrow, how did you get
                          that idea?


                          PointedEars
                          --
                          Use any version of Microsoft Frontpage to create your site.
                          (This won't prevent people from viewing your source, but no one
                          will want to steal it.)
                          -- from <http://www.vortex-webdesign.com/help/hidesource.htm>

                          Comment

                          • DL

                            #14
                            Re: Is it possible to create dynamic var

                            On May 11, 3:36 am, VK <schools_r...@y ahoo.comwrote:
                            On May 11, 7:32 am, DL <tatata9...@gma il.comwrote:
                            >
                            e.g. newCell1.style. align = "right";
                            >
                            align="left/right/center" is an attribute of the cell itself, not a
                            CSS rule.
                            Either:
                              newCell.align = 'right';
                            or
                              newCell.style.t extAlign = 'right';
                              // the latter is lesser functional than the first one
                            Thank you.

                            Comment

                            Working...