BlueSun3k1
BlueSun3k1

Reputation: 767

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.

Upvotes: 0

Views: 486

Answers (4)

Tomek B.
Tomek B.

Reputation: 145

If I understood what you want get, Try this:

for (var j = 1; j <= 12; j++)
    {
        var val;
        if (j<10)
            val="0"+j
        else
            val=j

        $('<option>', {value: val, text: m_names[j-1],slice(0,3)}).appendTo("#dob_month");
    }

Upvotes: 1

Sailesh Babu Doppalapudi
Sailesh Babu Doppalapudi

Reputation: 1544

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

Set the value of month to 1, 2, etc. This will be j+1 as j starts from 0

Upvotes: 1

Davide
Davide

Reputation: 62

Just change months to j+1 on value

like this

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

Upvotes: 1

Evan Taylor
Evan Taylor

Reputation: 191

If I'm understanding you right then this should work:

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

Upvotes: 1

Related Questions