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

setup an dynamic array

P: 12
Hi!

I'm all new into js so I'm kind of stuck with this script of mine.
I need it to fetch the choosen value in a dropdown menu when the user clicks an "Add" button. The value should then be stored somehow, so that the next time he clicks the same "Add" button, but maybe chooses another value from the drop-down, the script stores this value and so on. I've been thinking of doing it through an dynamic array, but I'm all stuck....Here's what I got so far..

Expand|Select|Wrap|Line Numbers
  1. <script>
  2. function addMailAddressses(){
  3.    var email = document.form.Mail.value
  4.    agree = confirm('Is the choosen e-mailaddress correct: ' + email )
  5.    if (agree){
  6.          alert('The address, ' + email + ' , has been added.')
  7.        var emails = newArray(9);
  8.     var i;
  9.  for (i = 1; i < 10; i++) {
  10. emails[i] = email + ';';
  11.  
  12.  
  13. }
  14.    else {
  15.    return false
  16.    }
  17. }
  18. </script> 
Jan 19 '08 #1
Share this Question
Share on Google+
3 Replies


acoder
Expert Mod 15k+
P: 16,027
Declare the array as
Expand|Select|Wrap|Line Numbers
  1. var emails = [];
and then just add as required:
Expand|Select|Wrap|Line Numbers
  1. emails[0] = someEmail;
Jan 21 '08 #2

P: 12
Thank you for your answer.

Is it correct to let the for-loop increase the array-index for me?

Have I done the code correctly or should I do it any differently?

The code should add one e-mailaddress to the array every time he puts in an address to the input-box and clicks the submit-button.

Eventually the whole array should be put into my DB.
Jan 21 '08 #3

acoder
Expert Mod 15k+
P: 16,027
Is it correct to let the for-loop increase the array-index for me?

Have I done the code correctly or should I do it any differently?

The code should add one e-mailaddress to the array every time he puts in an address to the input-box and clicks the submit-button.

Eventually the whole array should be put into my DB.
If you're only adding one address each time, the declaration of the array should be outside the function.

There's no need for a for loop because you're only adding one email address each time. You can use the length of the array to dynamically add a new item or use the array's push() method.

For storage in the database, you will need a server-side language.
Jan 22 '08 #4

Post your reply

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