rockdudex rockdudex - 5 months ago 13
jQuery Question

Hide incorrect days in droplist calendar

I'm quite new to web development and though I know I can use those calendar datepickers to input dates to a a form. I was wondering how can I hide or disable incorrect days from

day-month-year
droplists, like
30 february
or
31 april
. Is there a non obscure way to do that using javascript or jquery?
I'm thinking on this kind of datepicker.
See jsfiddle

Answer

Based on the answer from HERE, first add a few more classes to your drop-down lists and use JQuery this way:

$(document).ready(function () {
    $('.operator').on('change', function () {
    var daysx = Math.round(((new Date($('.year option:selected').val(), $('.month option:selected').val()))-(new Date($('.year option:selected').val(), parseInt($('.month option:selected').val())-1)))/86400000);

    console.log(daysx);
    $('.maindates option').remove();
    for (var i=1; i < (daysx + 1); i++) {
        $('.maindates').append('<option value="' + i + '">' + i + '</option>');
    }
  });
});

See my jsfiddle here: http://jsfiddle.net/fictus/2gc28dcd/

Comments