JS NOOB Alert - drop down date select box

B

Buzby

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!
 
S

scripts.contact

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)
}
 
L

-Lost

scripts.contact said:
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)
}

;)
 
B

Buzby

-Lost wibbled:
scripts.contact said:
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!
 
R

RobG

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top