Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery UI Datepicker after select/change month/year

I wanted to find out how I can run some piece of code (attach mouseenter event) for every day after user has selected a day or changed a month/year?

I have tried to attach the event on these events

  • beforeShow
  • beforeShowDay
  • onChangeMonthYear
  • onSelect

On hover I want to highlight next day in the same row if it exists.

Currently I attach moouseenter/mouseleave event to all days after datepicker is created (which is inline).

I have simplified what I'm doing in the JS fiddle below. I need to have those events working after a date is selected and after month/year has been changed.

JS Fiddle: http://jsfiddle.net/MartinTale/Xx4GS/2/

$("div").datepicker({
    changeMonth: true,
    changeYear: true,
    inline: true,
    altField: "#datep"
});

$("tbody td:not(.ui-state-disabled, .active-calendar-cell)").mouseenter(function (e) {
    $(this).closest('td').next().find("a").addClass("hover-calendar-cell");    
    console.log('test');
});

$("tbody td:not(.ui-state-disabled)").mouseleave(function (e) {
    $("a.hover-calendar-cell").removeClass("hover-calendar-cell");
    console.log('test out');
});

Thanks in advance.

like image 618
Martin Tale Avatar asked May 30 '14 09:05

Martin Tale


People also ask

How can change date format in jQuery UI Datepicker?

inside the jQuery script code just paste the code. $( ". selector" ). datepicker({ dateFormat: 'yy-mm-dd' });

How do I change date format in date picker?

Do one of the following: For a text box control or a date picker control, ensure that the Data type list displays the appropriate data type, and then click Format. For an expression box control, ensure that the Format as list displays the appropriate data type, and then click Format.

How do you only show month in date picker?

Set changeMonth and changeYear to true so that Month and Year appear as Dropdown. Set date format to "MM yy". jQuery DatePicker has "onClose" event, which is called when Datepicker gets closed. So using this event, fetch the selected Month and Year and setDate of Datepicker.

What is minDate and maxDate in jQuery Datepicker?

If you like to restrict access of users to select a date within a range then there is minDate and maxDate options are available in jQuery UI. Using this you can set the date range of the Datepicker. After defining these options the other days will be disabled which are not in a defined range.


2 Answers

You can use the provided jQuery datepicker events onSelect and onChangeMonthYear.

Code:

$("#datep").datepicker({
    changeMonth: true,
    changeYear: true,
    onSelect: function () {
        console.log('s');
    },
    onChangeMonthYear: function () {
        console.log('o');
    }
});

Demo: http://jsfiddle.net/IrvinDominin/Xx4GS/

like image 152
Irvin Dominin Avatar answered Oct 03 '22 03:10

Irvin Dominin


Here is hacky hack.

http://jsfiddle.net/Xx4GS/3/

It works, highlights the next day( the next td in this case).

setTimeout is because I think jquery ui destroys the active date item, so we wait until we have the right one.

You might be able to use onSelect instead of .change, but not a big deal imo

I left a console.log in there so you can see a little bit of whats going on.

like image 39
dansch Avatar answered Oct 03 '22 02:10

dansch