adding values to an array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • colinod
    Contributor
    • Nov 2007
    • 347

    adding values to an array

    I am trying to write an array that adda a number to the array on the click of a button, this is so i can send a series of numbers to a second page.

    im just not sure how to do it???

    never used arrays
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    I'm not aware that an array can write something to another array..... you probably mean function. you can use an array method (Array – MDC), e.g.
    Code:
    your_array.push(new_value);
    regards

    Comment

    • colinod
      Contributor
      • Nov 2007
      • 347

      #3
      what i need is the array to contain a series of numbers

      i.e 123,234,345 etc

      will the push function do this?

      The idea is that the array holds the id for things on our site so you can click them as a favorite and then view all favorites from another page....a bit like a shopping cart

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        let me explain the push() method:
        Code:
        var my_array = [1,2];
        // my_array now contains 1,2
        alert(my_array.length); // 2
        my_array.push(3);
        // my_array now contains 1,2,3
        alert(my_array.length); // 3
        note: you know that numbers are not allowed for IDs?

        regards

        Comment

        • colinod
          Contributor
          • Nov 2007
          • 347

          #5
          So i could do something like this

          Code:
          var my_array = []; 
          // my_array now contains nothing 
          my_array.push(123); 
          // my_array now contains 123
          then if i wanted to add another number
          Code:
          my_array.push(456); 
          // my_array now contains 123,456
          Is this correct???

          Also would you know how to get the amount stored in the array item from an asp variable

          eg the 123 or 456 to come from asp ID

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            Originally posted by colinod
            Is this correct???
            yes

            Originally posted by colinod
            Also would you know how to get the amount stored in the array item from an asp variable

            eg the 123 or 456 to come from asp ID
            sorry, I can't help with ASP, you should ask in the ASP forum for help

            Comment

            • colinod
              Contributor
              • Nov 2007
              • 347

              #7
              thankyou for your help

              Comment

              • Dormilich
                Recognized Expert Expert
                • Aug 2008
                • 8694

                #8
                I'm glad I could help.

                Comment

                • colinod
                  Contributor
                  • Nov 2007
                  • 347

                  #9
                  I have got the array being populated with an asp variable but how can i get the page to display the content of my_array, i have tried

                  Code:
                   
                  <script language="javascript"> 
                  document.write(my_array); 
                  </script>
                  in the body of the page but get nothing displayed
                  Last edited by Dormilich; Jan 23 '09, 05:14 PM. Reason: please use [code] tags instead of [quote] tags

                  Comment

                  • Dormilich
                    Recognized Expert Expert
                    • Aug 2008
                    • 8694

                    #10
                    you could try the Array.toString( ) or Array.toSource( ) method (see toString – MDC)

                    Comment

                    • colinod
                      Contributor
                      • Nov 2007
                      • 347

                      #11
                      I dont think thats quite what im looking for, i need a way of taking each entry into the array and having it appear in the html on my page, its just so i can see if the array is holding all the information

                      Comment

                      • Dormilich
                        Recognized Expert Expert
                        • Aug 2008
                        • 8694

                        #12
                        well, that's what the functions do, show you the content (i.e. returning the content)

                        Comment

                        Working...