faqts : Computers : Programming : Languages : JavaScript : Forms : SELECT

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

18 of 26 people (69%) answered Yes
Recently 5 of 10 people (50%) answered Yes

Entry

I want to populate a select box with number of days of the month selected. How can I do it?

Feb 19th, 2002 02:18
suresh kc, Sharon De Silva, Suresh KC


Here You Go
<!--START OF DOCUMENT-->
<html>
<head>
<script language=Javascript>
//this function is called when the body loads
//what this function does is gets the current date
//as the object d
//d.getDate() simply returns the day of the month
//and all we do after getting the day of the month is
//assigning it to the dayOfMonth element of the form
//which is our select box
//I am using date-1 in here because the array starts at 0
//but the date starts at 1
function selectDay() {
	var d = new Date();
	date = d.getDate();
	dateForm.dayOfMonth.selectedIndex = date-1;
}
</script>
</head>
<body onLoad="selectDay();">
<form name=dateForm>
<select name=dayOfMonth>
<script language=Javascript>
//didn't bother to write the same 31 times
for(var i=1; i<=31; i++) {
	document.write("<option value="+i+">"+i);
}
</script>
</select>
</form>
</body>
</html>
<!--END OF DOCUMENT-->
taa! 
enjoy