Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery Datepicker hide dates from one calendar on a page

I have several datepickers on the same page and need one to only display the month and year, while others should show complete calendar with days. I know that to hide days from a lone datepicker I can do something like:

<style>
 .ui-datepicker-calendar {
   display: none;
 }
</style>

But that hides the dates from all calendars. How can I make sure the other datepicker keeps the calendar view?

Edit I did try nesting CSS but am still missing something. For HTML part I have roughly:

<div id="monthOnly"><input type="text" class="monthly-picker" name="mdate"></div>

And JS code $('.monthly-picker').datepicker();

Still, doing something like

<style>
#monthOnly .ui-datepicker-calendar {
   display: none;
 }
</style>

Does not produce desired results.

like image 273
SaltyNuts Avatar asked Apr 27 '12 14:04

SaltyNuts


2 Answers

give the use an id on a container element.

<style>
 #monthOnly .ui-datepicker-calendar {
   display: none;
 }
</style>

EDIT: http://jsfiddle.net/h8DWR/ is a solution to deal with the idiosyncrasies of datepicker. The idea is use a style element to add the style when the specific datepicker is chosen and then remove it when it is closed.

like image 194
qw3n Avatar answered Sep 21 '22 12:09

qw3n


I did a little different ...

HTML

<label for="startDate">Date :</label>
<input name="startDate" id="startDate" class="date-picker" />

CSS

.hide-calendar .ui-datepicker-calendar {
    display: none;
}

jQuery

$(function() {
    $('.date-picker').datepicker( {
        changeMonth: true,
        changeYear: true,
        showButtonPanel: true,
        dateFormat: 'MM yy',
        yearRange: '1980:' + new Date().getFullYear(),
        beforeShow: function(el, dp) { 
            $('#ui-datepicker-div').addClass('hide-calendar');
        },
        onClose: function(dateText, inst) { 
            $('#ui-datepicker-div').removeClass('hide-calendar');
            var month = $("#ui-datepicker-div .ui-datepicker-month :selected").val();
            var year = $("#ui-datepicker-div .ui-datepicker-year :selected").val();
            $(this).datepicker('setDate', new Date(year, month, 1));
        }
    });
});

Fiddle

like image 39
Michel Ayres Avatar answered Sep 20 '22 12:09

Michel Ayres