user1582396 user1582396 - 3 months ago 10
HTML Question

PHP-jQuery - How to populate dropdown with month name and set value to month number

I'm trying to populate a dropdown list with month names, another one with days and another one with years and everything is fine except that I'm having issues setting the to be the month number instead of the month name.

this is what I have.

<script>
$('document').ready(function()
{
var minOffset = -70, maxOffset = -17;
var thisYear = (new Date()).getFullYear();
var m_names = ['January', 'February', 'March','April', 'May', 'June', 'July','August', 'September', 'October','November', 'December'];
var month = 0;

for (var j = month; j <= 11; j++)
{
var months = m_names[ 0 + j].slice( 0, 3 );
$('<option>', {value: months, text: months}).appendTo("#dob_month");
}

for (var i = minOffset; i <= maxOffset; i++)
{
var year = thisYear + i;
$('<option>', {value: year, text: year}).appendTo("#dob_year");
}
});
</script>


The days options are currently hard-coded in the form 1-31 (might need help with this to populate the correct amount of days for each month, February always being 29).

How can I set the option value to be for example 01 when January is picked?

I am open to any other suggestions besides this. Just please keep in mind that I will eventually concatenate year-month-day to check against a date in the database.

Answer

Just change months to j+1 on value

like this

$('<option>', {value: j+1, text: months}).appendTo("#dob_month");
Comments