setup an dynamic array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dudelideisann
    New Member
    • Jan 2008
    • 12

    setup an dynamic array

    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..

    Code:
    <script>
    function addMailAddressses(){
       var email = document.form.Mail.value
       agree = confirm('Is the choosen e-mailaddress correct: ' + email )
       if (agree){
             alert('The address, ' + email + ' , has been added.')
       	var emails = newArray(9);
    	var i;
     for (i = 1; i < 10; i++) {
    emails[i] = email + ';';
    
    
    }
       else {
       return false
       }
    }
    </script>
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Declare the array as
    Code:
    var emails = [];
    and then just add as required:
    Code:
    emails[0] = someEmail;

    Comment

    • dudelideisann
      New Member
      • Jan 2008
      • 12

      #3
      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.

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        Originally posted by dudelideisann
        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.

        Comment

        Working...