By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,256 Members | 1,700 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,256 IT Pros & Developers. It's quick & easy.

adding values to an array

100+
P: 347
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
Jan 22 '09 #1
Share this Question
Share on Google+
11 Replies


Dormilich
Expert Mod 5K+
P: 8,639
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.
Expand|Select|Wrap|Line Numbers
  1. your_array.push(new_value);
regards
Jan 22 '09 #2

100+
P: 347
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
Jan 22 '09 #3

Dormilich
Expert Mod 5K+
P: 8,639
let me explain the push() method:
Expand|Select|Wrap|Line Numbers
  1. var my_array = [1,2];
  2. // my_array now contains 1,2
  3. alert(my_array.length); // 2
  4. my_array.push(3);
  5. // my_array now contains 1,2,3
  6. alert(my_array.length); // 3
note: you know that numbers are not allowed for IDs?

regards
Jan 22 '09 #4

100+
P: 347
So i could do something like this

Expand|Select|Wrap|Line Numbers
  1. var my_array = []; 
  2. // my_array now contains nothing 
  3. my_array.push(123); 
  4. // my_array now contains 123
  5.  
then if i wanted to add another number
Expand|Select|Wrap|Line Numbers
  1. my_array.push(456); 
  2. // my_array now contains 123,456
  3.  
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
Jan 22 '09 #5

Dormilich
Expert Mod 5K+
P: 8,639
@colinod
yes

@colinod
sorry, I can't help with ASP, you should ask in the ASP forum for help
Jan 22 '09 #6

100+
P: 347
thankyou for your help
Jan 22 '09 #7

Dormilich
Expert Mod 5K+
P: 8,639
I'm glad I could help.
Jan 22 '09 #8

100+
P: 347
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

Expand|Select|Wrap|Line Numbers
  1.  
  2. <script language="javascript"> 
  3. document.write(my_array); 
  4. </script>
  5.  
in the body of the page but get nothing displayed
Jan 23 '09 #9

Dormilich
Expert Mod 5K+
P: 8,639
you could try the Array.toString() or Array.toSource() method (see toString MDC)
Jan 23 '09 #10

100+
P: 347
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
Jan 23 '09 #11

Dormilich
Expert Mod 5K+
P: 8,639
well, that's what the functions do, show you the content (i.e. returning the content)
Jan 23 '09 #12

Post your reply

Sign in to post your reply or Sign up for a free account.