can someone give me the code to display drop down date with default todays date selected...format dd/mm/yyyy
i found the code to display month in mon format..what is to be modified in the function to get in mm format here
______________

<script>
var monthtext=['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sept','Oct','Nov','Dec'];

function populatedropdown(dayfield, monthfield, yearfield){
var today=new Date()
var dayfield=document.getElementById(dayfield)
var monthfield=document.getElementById(monthfield)
var yearfield=document.getElementById(yearfield)
for (var i=0; i<31; i++)
dayfield.options[i]=new Option(i, i+1)
dayfield.options[today.getDate()]=new Option(today.getDate(), today.getDate(), true, true) //select today's day
for (var m=0; m<12; m++)
monthfield.options[m]=new Option(monthtext[m], monthtext[m])
monthfield.options[today.getMonth()]=new Option(monthtext[today.getMonth()], monthtext[today.getMonth()], true, true) //select today's month
var thisyear=today.getFullYear()
for (var y=0; y<20; y++){
yearfield.options[y]=new Option(thisyear, thisyear)
thisyear+=1
}
yearfield.options[0]=new Option(today.getFullYear(), today.getFullYear(), true, true) //select today's year
}

</script>

Recommended Answers

All 3 Replies

<script>
var monthtext=['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sept','Oct','Nov','Dec'];

function populatedropdown(dayfield, monthfield, yearfield){
	var today=new Date()
	var dayfield=document.getElementById(dayfield);
	var monthfield=document.getElementById(monthfield);
	var yearfield=document.getElementById(yearfield);

	for (var i=0; i<31; i++)
		dayfield.options[i]=new Option(i+1, i+1)
	dayfield.options[today.getDate()-1].selected=true;

	for (var m=0; m<12; m++)
		monthfield.options[m]=new Option(monthtext[m], monthtext[m])
	monthfield.options[today.getMonth()].selected=true;
	var thisyear=today.getFullYear()
	for (var y=0; y<20; y++){
		yearfield.options[y]=new Option(thisyear, thisyear)
		thisyear+=1
	}
yearfield.options[0]=new Option(today.getFullYear(), today.getFullYear(), true, true) //select today's year
}

onload=function(){
	 populatedropdown('d', 'm', 'y')
};
</script>
<select id='m' name='month'></select>
<select id='d' name='day'></select>
<select id='y' name='year'></select>

hi the code works good for me but if i had others select field, for example, check out date, these new fields aren't filled. why?

If you are trying to create multiple "sets" of dropdowns, make sure you give all your selects unique ids and call the function with the corresponding ids -ex:

onload=function(){
	 populatedropdown('d1', 'm1', 'y1')
	 populatedropdown('d2', 'm2', 'y2')
};
</script>
<div>
<select id='m1' name='month'></select>
<select id='d1' name='day'></select>
<select id='y1' name='year'></select>
</div>
<div>
<select id='m2' name='month'></select>
<select id='d2' name='day'></select>
<select id='y2' name='year'></select>
</div>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.