www.webdeveloper.com
Results 1 to 4 of 4

Thread: Adding options to an array Javascript

Hybrid View

  1. #1
    Join Date
    Dec 2008
    Posts
    11

    Question Adding options to an array Javascript

    Hi,

    I want to create a simple form where the user inputs their birthdate. There are three drop downs. One for month, one for day and for year. I would like to do it with js and arrays and not have to list all the options for days and years. I found examples for how to do the months. (see below)

    What I want to know is how do I create an array for the days and years without having to list them out? It seems like there should be a way to define an array to start with one and end with 31? or 1900 - 2000? Any advice, guidance will be greatly appreciated. I see many birthdate input fields forms are they done manually?


    <script language="javascript" >

    function addOption(selectbox,text,value )
    {
    var optn = document.createElement("OPTION");
    optn.text = text;
    optn.value = value;
    selectbox.options.add(optn);
    }


    function addOption_list(selectbox){

    var month = new Array("January","February","March","April","May","June","July","August","September","October","Novem ber","December");
    for (var i=0; i < month.length;++i){


    addOption(document.drop_list.Month_list, month[i], month[i]);
    }
    }
    </script>

    <body onLoad="addOption_list()";>

    <FORM name="drop_list" action="" method="POST" >

    <SELECT NAME="Month_list">
    <Option value="" >Month list</option>
    </SELECT>
    </form>

    The example came from here: http://www.plus2net.com/javascript_t...ist-adding.php

    Cheers,
    Techie35

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,415

    Lightbulb Consider this ...

    Is this what you had in mind?
    Code:
    <html>
    <head>
    <title>Birthdate Selection</title>
    <script type="text/javascript" language="javascript" >
    // From: http://www.webdeveloper.com/forum/showthread.php?t=199300
    
    function addOption(selectbox,text,value ) {
      var optn = document.createElement("OPTION");
      optn.text = text;
      optn.value = value;
      selectbox.options.add(optn);
    }
    
    function addOption_list(selectbox) {
      var month = new Array("January","February","March","April","May","June",
                            "July","August","September","October","Novem ber","December");
      for (var i=0; i < month.length;++i){
        addOption(document.drop_list.Month_list, month[i], month[i]);
      }
      for (var i=1; i<32; ++i){
        addOption(document.drop_list.Date_list, i, i);
      }
      for (var i=1940; i<2010; ++i){
        addOption(document.drop_list.Year_list, i, i);
      }
    }
    </script>
    
    <body onLoad="addOption_list()";>
    
    <FORM name="drop_list" action="" method="POST" onsubmit="return false">
     <SELECT NAME="Month_list"><Option value="" >Month list</option></SELECT>
     <SELECT NAME="Date_list"><Option value="" >Date list</option></SELECT>
     <SELECT NAME="Year_list"><Option value="" >Year list</option></SELECT>
    </form>
    </body>
    </html>

  3. #3
    Join Date
    Dec 2008
    Posts
    11

    resolved Thank JMRKR

    THANK YOU!!!!! This exactly what I need. Greatly appreciate!!!!! Have a wonderful Monday. You made my day!

  4. #4
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,415
    You're most welcome.
    Glad I was able to help.
    Good Luck!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles