John
John

Reputation: 1629

Populate select with loop

I would like to repopulate the select box on select change.

What i did so far (does not work):

$('select#mnt').change(function(){

     var month = $(this).val();
     var days = new Date(year, month, 0).getDate();
     var toMonth = <?php echo $tomon; ?>;
     var toDay = <?php echo $today; ?>;
     var result='';

     if (month!=toMonth) {

        for(var i=1; i<=days;i++){

            result += '<option>'+i+'</option>';

        }

        $('select#days').empty().html(result);
     }




  });

Upvotes: 0

Views: 1244

Answers (2)

Bellash
Bellash

Reputation: 8184

The year var does not exist in your code snippet:

var days = new Date(year, month, 0).getDate();

It should be something like this:

var year=2011;//or $('input#year').val();
var days = new Date(year, month, 0).getDate();

Upvotes: 2

Niklas
Niklas

Reputation: 13145

You don't have the year variable in that function. Do you create it elsewhere?

Here's a working example, with some changes: DEMO

$('select#mnt').change(function(){

 var month = $(this).val();
 var days = new Date(2012, month, 0).getDate();
 var toMonth = 5;
 var toDay = 4;
 var result='';

 if (month!=toMonth) {

    for(var i=1; i<=days;i++){

        result += '<option>'+i+'</option>';
    }

    $('select#days').empty().html(result);
 }
});

Upvotes: 1

Related Questions