JS NOOB Alert - drop down date select box

Discussion in 'Javascript' started by Buzby, Jun 18, 2007.

  1. Buzby

    Buzby Guest

    I want a drop down date selection box on a form in the format of Mon 18
    June 2007, that will go say 400 days in advance from the current date.

    Can anyone point me in the right direction as I appear to be going in
    ever decreasing circles . . .

    TIA!

    --
    Buzby
    There's nothing more dangerous than a resourceful idiot
    Buzby, Jun 18, 2007
    #1
    1. Advertising

  2. On Jun 18, 8:21 am, "Buzby" <> wrote:
    > I want a drop down date selection box on a form in the format of Mon 18
    > June 2007, that will go say 400 days in advance from the current date.
    >



    function generateSelect(){
    var sel=document.createElement("select"),opt,dt;
    var date=new Date();date.setDate(date.getDate()-1);
    var days=['Sun','Mon','Tue','Wed','Thur','Fri','Sat']
    var
    months=['Jan','Feb','Mar','Apr','May','June','July','Aug','Sep','Oct','Nov','Dec']
    for(var i=0;i<400;i++){
    date.setDate(date.getDate()+1);
    opt=document.createElement("option");
    opt.text=days[date.getDay()]+" "+date.getDate()+"
    "+months[date.getMonth()]+" "+date.getYear()
    sel.add(opt)
    }
    document.body.appendChild(sel)
    }
    scripts.contact, Jun 18, 2007
    #2
    1. Advertising

  3. Buzby

    -Lost Guest

    scripts.contact wrote:
    > On Jun 18, 8:21 am, "Buzby" <> wrote:
    >> I want a drop down date selection box on a form in the format of Mon 18
    >> June 2007, that will go say 400 days in advance from the current date.
    >>

    >
    >
    > function generateSelect(){
    > var sel=document.createElement("select"),opt,dt;
    > var date=new Date();date.setDate(date.getDate()-1);
    > var days=['Sun','Mon','Tue','Wed','Thur','Fri','Sat']
    > var
    > months=['Jan','Feb','Mar','Apr','May','June','July','Aug','Sep','Oct','Nov','Dec']
    > for(var i=0;i<400;i++){
    > date.setDate(date.getDate()+1);
    > opt=document.createElement("option");
    > opt.text=days[date.getDay()]+" "+date.getDate()+"
    > "+months[date.getMonth()]+" "+date.getYear()


    "+months[date.getMonth()]+" "+date.getFullYear()

    > sel.add(opt)


    sel.appendChild(opt);

    > }
    > document.body.appendChild(sel)
    > }


    ;)

    --
    -Lost
    Remove the extra words to reply by e-mail. Don't e-mail me. I am
    kidding. No I am not.
    -Lost, Jun 18, 2007
    #3
  4. Buzby

    Buzby Guest

    -Lost wibbled:

    > scripts.contact wrote:
    > >On Jun 18, 8:21 am, "Buzby" <> wrote:
    > > > I want a drop down date selection box on a form in the format of
    > > > Mon 18 June 2007, that will go say 400 days in advance from the
    > > > current date.
    > > >

    > >
    > >
    > > function generateSelect(){
    > > var sel=document.createElement("select"),opt,dt;
    > > var date=new Date();date.setDate(date.getDate()-1);
    > > var days=['Sun','Mon','Tue','Wed','Thur','Fri','Sat']
    > > var
    > > months=['Jan','Feb','Mar','Apr','May','June','July','Aug','Sep','Oct
    > > ','Nov','Dec'] for(var i=0;i<400;i++){
    > > date.setDate(date.getDate()+1);
    > > opt=document.createElement("option");
    > > opt.text=days[date.getDay()]+" "+date.getDate()+"
    > > "+months[date.getMonth()]+" "+date.getYear()

    >
    > "+months[date.getMonth()]+" "+date.getFullYear()
    >
    > > sel.add(opt)

    >
    > sel.appendChild(opt);
    >
    > > }
    > > document.body.appendChild(sel)
    > > }

    >
    > ;)


    Thanks for the replies - still not quite there having tried both your
    suggestions this one is nearly there (lines wrapped):-

    function generateSelect(){
    var sel=document.createElement("select"),opt,dt;
    var date=new Date();date.setDate(date.getDate()-1);
    var days=['Sun','Mon','Tue','Wed','Thur','Fri','Sat']
    var
    months=['Jan','Feb','Mar','Apr','May','June','July','Aug','Sep','Oct','N
    ov','Dec']
    for(var i=0;i<400;i++){
    date.setDate(date.getDate()+1);
    opt=document.createElement("option");
    opt.text=days[date.getDay()]+" "+date.getDate()+"
    "+months[date.getMonth()]+" "+date.getFullYear()
    sel.add(opt)
    }
    document.body.appendChild(sel)
    }


    When I call the function it displays the drop down menu perfectly but
    has an error message next to it saying 'undefined'. If I change the
    sel.add(opt) line to sel.appendChild(opt); I get an empty box with
    undefined also.

    I just don't get which bit is undefined!

    Thanks - much appreciated!
    --
    Buzby
    There's nothing more dangerous than a resourceful idiot
    Buzby, Jun 19, 2007
    #4
  5. Buzby

    RobG Guest

    On Jun 19, 8:14 pm, "Buzby" <> wrote:
    [...]
    > Thanks for the replies - still not quite there having tried both your
    > suggestions this one is nearly there (lines wrapped):-


    I can't say I agree with a select with 400 options, but anyhow...


    > function generateSelect(){
    > var sel=document.createElement("select"),opt,dt;
    > var date=new Date();date.setDate(date.getDate()-1);
    > var days=['Sun','Mon','Tue','Wed','Thur','Fri','Sat']
    > var
    > months=['Jan','Feb','Mar','Apr','May','June','July','Aug','Sep','Oct','N
    > ov','Dec']
    > for(var i=0;i<400;i++){
    > date.setDate(date.getDate()+1);


    Replace from here...

    > opt=document.createElement("option");
    > opt.text=days[date.getDay()]+" "+date.getDate()+"
    > "+months[date.getMonth()]+" "+date.getFullYear()
    > sel.add(opt)


    to here with:

    sel.options = new Option( days[date.getDay()]
    + " " + date.getDate() + " "
    + months[date.getMonth()] + " "
    + date.getFullYear();

    > }
    > document.body.appendChild(sel)
    >
    > }


    Alternatively, you can use createElement and append the option text by
    creating a text node and appending that rather than setting the text
    attribute, but I think new Option is simpler.


    --
    Rob
    RobG, Jun 19, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. SirPoonga
    Replies:
    2
    Views:
    744
    Ben Strackany
    Jan 7, 2005
  2. weiwei
    Replies:
    0
    Views:
    1,002
    weiwei
    Jan 5, 2007
  3. Sy Ys
    Replies:
    0
    Views:
    121
    Sy Ys
    Sep 15, 2007
  4. palmiere
    Replies:
    1
    Views:
    388
    Erwin Moller
    Feb 9, 2004
  5. Replies:
    3
    Views:
    276
Loading...

Share This Page