kirkharamiler kirkharamiler - 7 months ago 34
Javascript Question

Jquery BeforeShowDay pick even or odd

i was trying something but i cant figure out problem. i have calendar. If i choose even month only even dates should be pickable and if i choose odd month only odd dates should be pickable. but my problem when i choose odd month it shows all dates and when i choose even month it doesn't show any date.
http://jsfiddle.net/66688hft/1/

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Datepicker - Default functionality</title>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/ui-lightness/jquery-ui.css" type="text/css" rel="stylesheet" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css">
<script>
$(function() {
$( "#datepicker" ).datepicker({
changeMonth: true,
changeYear: true,
showOn: "button",
buttonImage: "calendar.gif",
buttonImageOnly: true,
buttonText: "Select date",
beforeShowDay: function(date) {
var day = $(this).datepicker('getDate');
var month = date.getMonth()+1;
if(month%2==1)
return [day % 2 == 1];

else
return [day % 2 == 0];

}

});
});
</script>
</head>
<body>

<p>Date: <input type="text" id="datepicker"></p>


</body>
</html>

Answer

David is correct, you aren't getting the day correctly. Try this:

$(function() {
  $( "#datepicker" ).datepicker({
      changeMonth: true,
      changeYear: true,
      showOn: "button",
      buttonImage: "calendar.gif",
      buttonImageOnly: true,
      buttonText: "Select date",
      beforeShowDay: function(date) {
          var day = date.getDate(),
              month = date.getMonth()+1;
          return [day % 2 == month % 2, ''];
      }
  });
}