Basic array stuff

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

    Basic array stuff

    This is kind of basic, and I googled but didn't find much help.

    I have an array of text element fields. They all need to have the same
    name. So, let the name be either all with a[] or build individually as
    a[0], a[1], a[2], etc. The brackets need to be there because I will
    obtaining the values of them as an array using $_POST{'a'] in php.

    What I want to do is in javascript set the value of an individual
    element, say a[3]. I have tried many combinations of

    1 - setting the ids all as
    a) a
    b) a[]
    c) individually as a[0], a[1], etc.
    2 - using document.getEle mentById
    3 - using document.getEle mentsByName

    Any time I create and object from the name, and then try to use that
    object, I get "object has no properties" in Firebug. This is true
    whether I try obj.value or obj[i].value.

    So, please,
    1 - How do I set the id attribute?
    2 - How do I access or set an individual member in the array?
    3 - For the name attribute, should I use a[] or individually build them
    or does it not matter as far as javascript is concerned?

    Yes, this is basic, but I have been going in circles for a few hours now
    so any help will be appreciated.
  • richard

    #2
    Re: Basic array stuff

    On Sun, 05 Oct 2008 15:38:49 -0400, sheldonlg <sheldonlgwrote :
    >This is kind of basic, and I googled but didn't find much help.
    >
    >I have an array of text element fields. They all need to have the same
    >name. So, let the name be either all with a[] or build individually as
    >a[0], a[1], a[2], etc. The brackets need to be there because I will
    >obtaining the values of them as an array using $_POST{'a'] in php.
    >
    >What I want to do is in javascript set the value of an individual
    >element, say a[3]. I have tried many combinations of
    >
    >1 - setting the ids all as
    > a) a
    > b) a[]
    > c) individually as a[0], a[1], etc.
    >2 - using document.getEle mentById
    >3 - using document.getEle mentsByName
    >
    >Any time I create and object from the name, and then try to use that
    >object, I get "object has no properties" in Firebug. This is true
    >whether I try obj.value or obj[i].value.
    >
    >So, please,
    >1 - How do I set the id attribute?
    >2 - How do I access or set an individual member in the array?
    >3 - For the name attribute, should I use a[] or individually build them
    >or does it not matter as far as javascript is concerned?
    >
    >Yes, this is basic, but I have been going in circles for a few hours now
    >so any help will be appreciated.
    Pleast post question to comp.lang.javas cript.
    This group is about basic html.


    Comment

    • Dr J R Stockton

      #3
      Re: Basic array stuff

      On Oct 5, 9:17 pm, richard <mem...@newsguy .comwrote:
      On Sun, 05 Oct 2008 15:38:49 -0400, sheldonlg <sheldonlgwrote :
      >>
      Pleast post question to comp.lang.javas cript.
      This group is about basic html.- Hide quoted text -

      For one of the two values of this, it is. Check where you are
      writing.

      --
      (c) John Stockton, near London, UK. Posting with Google.
      Mail: J.R.""""""""@ph ysics.org or (better) via Home Page at
      Web: <URL:http://www.merlyn.demo n.co.uk/>
      FAQish topics, acronyms, links, etc.; Date, Delphi, JavaScript, ....|

      Comment

      • Jim Moe

        #4
        Re: Basic array stuff

        On 10/05/08 12:38 pm, sheldonlg wrote:
        >
        I have an array of text element fields. They all need to have the same
        name. So, let the name be either all with a[] or build individually as
        a[0], a[1], a[2], etc. The brackets need to be there because I will
        obtaining the values of them as an array using $_POST{'a'] in php.
        >
        How is that "array of text fields" created in HTML?
        What I want to do is in javascript set the value of an individual
        element, say a[3]. I have tried many combinations of
        >
        PHP is a server-side program. Javascript is a client-side language. What
        exists in PHP on the server is not accessible by the client.
        Using document.getEle mentById() is ineffective since the ID attribute
        must be unique within a document.
        Using document.getEle mentsByName() might work by calling it repeatedly
        until a null is returned. But not all browsers work that way. (ISTR some
        browsers return only the first instance, others only the last, yet others
        all of them.)
        BTW: What happens for a client that has JS disabled?
        >
        Any time I create an object from the name, and then try to use that
        object, I get "object has no properties" in Firebug. This is true
        whether I try obj.value or obj[i].value.
        >
        An URL to a test case would be most useful.

        --
        jmm (hyphen) list (at) sohnen-moe (dot) com
        (Remove .AXSPAMGN for email)

        Comment

        • sheldonlg

          #5
          Re: Basic array stuff

          I finally got it to where it works (www.sheldonlg.com/t.php)

          The simple answer was to have the names and ids all as "a[]". The
          javascript does:

          obj = document.getEle mentsByName('t[]');

          and the values are at obj[i].value.

          Comment

          • Jim Moe

            #6
            Re: Basic array stuff

            On 10/05/08 06:10 pm, sheldonlg wrote:
            I finally got it to where it works (www.sheldonlg.com/t.php)
            >
            Oddly, the "js" button does nothing. Is it supposed to?
            The simple answer was to have the names and ids all as "a[]". The
            javascript does:
            >
            And is invalid markup. IDs are required to be unique within a document.

            --
            jmm (hyphen) list (at) sohnen-moe (dot) com
            (Remove .AXSPAMGN for email)

            Comment

            • Thomas 'PointedEars' Lahn

              #7
              Re: Basic array stuff

              Jim Moe <jmm-list.AXSPAMGN@s ohnen-moe.comwrote:
              sheldonlg wrote:
              >I finally got it to where it works  (www.sheldonlg.com/t.php)
              >
                Oddly, the "js" button does nothing. Is it supposed to?
              It alerts 11 and 22 in a row here. Maybe it does work work with you
              because the markup is not Valid, duplicate IDs aside:

              <http://validator.w3.org/check?verbos...uri=http%3A%2F
              %2Fwww.sheldonl g.com%2Ft.php>
              The simple answer was to have the names and ids all as "a[]".  The
              javascript does:
              >
                And is invalid markup. IDs are required to be unique within a document.
              But not names.

              BTW, you should fix your borken sender address so as to comply with
              Internet standards and Netiquette.


              F'up2 cljs

              PointedEars

              Comment

              • sheldonlg

                #8
                Re: Basic array stuff

                Thomas 'PointedEars' Lahn wrote:
                Jim Moe <jmm-list.AXSPAMGN@s ohnen-moe.comwrote:
                >sheldonlg wrote:
                >>I finally got it to where it works (www.sheldonlg.com/t.php)
                > Oddly, the "js" button does nothing. Is it supposed to?
                >
                It alerts 11 and 22 in a row here. Maybe it does work work with you
                because the markup is not Valid, duplicate IDs aside:
                >
                <http://validator.w3.org/check?verbos...uri=http%3A%2F
                %2Fwww.sheldonl g.com%2Ft.php>
                >
                >>The simple answer was to have the names and ids all as "a[]". The
                >>javascript does:
                > And is invalid markup. IDs are required to be unique within a document.
                >
                But not names.
                >
                BTW, you should fix your borken sender address so as to comply with
                Internet standards and Netiquette.
                >
                >
                F'up2 cljs
                >
                PointedEars
                I fixed a couple of errors.

                1 - Yes, the ids of t[] can all be removed as there is no need for these
                ids and ids should not be duplicates. I used getElementsByNa me.

                2 - The complaint about the semi-colon in
                for (i=0; i<obj.length; i++) {
                is weird. That occurs between a <script></scriptinside the javascript
                function. That is a proper specification for a for-loop. Why did it
                complain? Here is the function:

                <script type="text/javascript">
                function dojs() {
                var obj = document.getEle mentsByName('t[]');
                for (i=0; i<obj.length; i++) {
                alert('t[' + i + '] = ' + obj[i].value);
                }
                }
                </script>

                Comment

                • Thomas 'PointedEars' Lahn

                  #9
                  Re: Basic array stuff

                  sheldonlg <sheldonlgwrote :
                  Thomas 'PointedEars' Lahn wrote:>
                  [...]
                  2 - The complaint about the semi-colon in
                          for (i=0; i<obj.length; i++) {
                  You did to declare `i', which is error-prone, and you have a property
                  resolved repeatedly, which is inefficient:

                  for (var i = 0, len = obj.lengh; i < len; i++)
                  {
                  is weird.  That occurs between a <script></scriptinside the javascript
                  function.  That is a proper specification for a for-loop.  Why did it
                  complain?
                  Because in XHTML the content model of the `script' element is PCDATA,
                  not CDATA as in HTML. Therefore, `<' is regarded an STAGO (Start Tag
                  Open) delimiter and `;' is not part of a proper attribute name that is
                  expected in a start tag.

                  While the solutions for this "problem" are simple and have been
                  discussed also here several times -- Google is your friend [psf 6.1]
                  --, you should simply not declare XHTML when you don't know what you
                  are doing.

                  And apparently your sender header is borken as well. Fix it.


                  PointedEars

                  Comment

                  • Jim Moe

                    #10
                    Re: Basic array stuff

                    On 10/06/08 02:32 am, Thomas 'PointedEars' Lahn wrote:
                    >
                    BTW, you should fix your borken sender address so as to comply with
                    Internet standards and Netiquette.
                    >
                    Please be more specific.

                    --
                    jmm (hyphen) list (at) sohnen-moe (dot) com
                    (Remove .AXSPAMGN for email)

                    Comment

                    • sheldonlg

                      #11
                      Re: Basic array stuff

                      Thomas 'PointedEars' Lahn wrote:
                      sheldonlg <sheldonlgwrote :
                      >Thomas 'PointedEars' Lahn wrote:
                      >><http://validator.w3.org/check?verbos...uri=http%3A%2F
                      >>%2Fwww.sheldo nlg.com%2Ft.php >
                      >[...]
                      >2 - The complaint about the semi-colon in
                      > for (i=0; i<obj.length; i++) {
                      >
                      You did to declare `i', which is error-prone, and you have a property
                      resolved repeatedly, which is inefficient:
                      OK, I can but the resolving being inefficient and should have simply set
                      it once. However, I just don't understand what you are saying about

                      `i'

                      Where did I use that accent mark before the "i"?


                      >
                      for (var i = 0, len = obj.lengh; i < len; i++)
                      {
                      >
                      I think you have a typo here.
                      >is weird. That occurs between a <script></scriptinside the javascript
                      >function. That is a proper specification for a for-loop. Why did it
                      >complain?
                      >
                      Because in XHTML the content model of the `script' element is PCDATA,
                      not CDATA as in HTML. Therefore, `<' is regarded an STAGO (Start Tag
                      Open) delimiter and `;' is not part of a proper attribute name that is
                      expected in a start tag.
                      I don't understand what you are saying. Do you mean that because it is
                      defined as xhtml, that even though it is inside a javascript it treats
                      the "<" as an opening of a tag rather than as a "less than"? Is so,
                      then why would your suggestion abovenot have a complaint?
                      >
                      While the solutions for this "problem" are simple and have been
                      discussed also here several times -- Google is your friend [psf 6.1]
                      --, you should simply not declare XHTML when you don't know what you
                      are doing.
                      Apparently my default template in Dreamweaver for a new PHP file
                      produces that. I didn't pay much attention to it when I composed the
                      example case.

                      Comment

                      • Gregor Kofler

                        #12
                        Re: Basic array stuff

                        sheldonlg meinte:
                        OK, I can but the resolving being inefficient and should have simply set
                        it once. However, I just don't understand what you are saying about
                        >
                        `i'
                        >
                        Where did I use that accent mark before the "i"?
                        You should add *var* to declare it as a local variable. Perhaps you
                        should try JSLint to "validate" your scripts. It's a bit harsh on code
                        not following its guidelines, but a great tool to find such issues.
                        > for (var i = 0, len = obj.lengh; i < len; i++)
                        I think you have a typo here.
                        Yes, it should read "length".
                        >Because in XHTML the content model of the `script' element is PCDATA,
                        >not CDATA as in HTML. Therefore, `<' is regarded an STAGO (Start Tag
                        >Open) delimiter and `;' is not part of a proper attribute name that is
                        >expected in a start tag.
                        >
                        I don't understand what you are saying. Do you mean that because it is
                        defined as xhtml, that even though it is inside a javascript it treats
                        the "<" as an opening of a tag rather than as a "less than"?
                        Yes.

                        Gregor


                        --
                        http://photo.gregorkofler.at ::: Landschafts- und Reisefotografie
                        http://web.gregorkofler.com ::: meine JS-Spielwiese
                        http://www.image2d.com ::: Bildagentur für den alpinen Raum

                        Comment

                        • Thomas 'PointedEars' Lahn

                          #13
                          Re: Basic array stuff

                          Thomas 'PointedEars' Lahn wrote:
                          sheldonlg <sheldonlgwrote :
                          > for (i=0; i<obj.length; i++) {
                          JFTR:
                          You did to declare `i', which is error-prone, and you have a property
                          s/to/not/
                          resolved repeatedly, which is inefficient:
                          >
                          for (var i = 0, len = obj.lengh; i < len; i++)
                          {
                          for (var i = 0, len = obj.length; i < len; i++)
                          {


                          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

                          Working...