424,279 Members | 1,905 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,279 IT Pros & Developers. It's quick & easy.

JS NOOB Alert - drop down date select box

P: n/a

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
Jun 18 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Jun 18, 8:21 am, "Buzby" <g...@pumpupthe.netwrote:
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)
}

Jun 18 '07 #2

P: n/a
scripts.contact wrote:
On Jun 18, 8:21 am, "Buzby" <g...@pumpupthe.netwrote:
>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.
Jun 18 '07 #3

P: n/a
-Lost wibbled:
scripts.contact wrote:
On Jun 18, 8:21 am, "Buzby" <g...@pumpupthe.netwrote:
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
Jun 19 '07 #4

P: n/a
On Jun 19, 8:14 pm, "Buzby" <g...@pumpupthe.netwrote:
[...]
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[i] = 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

Jun 19 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.