Datepicker: Add option for onUpdateDatepicker callback#1912
Conversation
Add a new option named onUpdateDatepicker that allows a custom callback to be provided. If provided, the callback is called at the end of $.datepicker._updateDatepicker.
|
Interesting PR! Seems like a valid use case and a proper approach. Docs need a PR against https://github.com/jquery/api.jqueryui.com/blob/main/entries/datepicker.xml @mgol I'm fine with this PR. What do you think? How to deal with the needed docs update? Too bad the rewrite has never been released. It's using the widget factory and is much easier to extend :-( |
@fnagel I cloned api.jqueryui.com and started updating But looking at |
Make sure the custom element added by the onUpdateDatepicker callback still exists and is not duplicated after calling refresh and setDate.
@fnagel I've updated the commit message 🙂 |
|
@patrick-vandy I did a bit of research and it seems code examples are generated based on metadata using one of the patterns from the https://github.com/jquery/grunt-jquery-content repository. This particular example is using this pattern: I'd start with making sure the new option is properly documented in the signatures section in |
|
@fnagel @mgol Okay, I've opened jquery/api.jqueryui.com#334 to add this new option to the documentation. Let me know if there's anything else that needs to happen. Also, I just realized on this PR I committed to |
|
@patrick-vandy No need to change the source branch and the target branch has been changed from master to main by GH. |

Add a new option named
onUpdateDatepickerthat allows a custom callback to be provided. If provided, the callback is called at the end of$.datepicker._updateDatepicker.Problem
Currently, there is not a good way to manipulate the DOM for the datepicker calendar. Using a common example, if you want to add a custom button, you have to do it in a callback on both
beforeShowandonChangeMonthYear.If using an inline datepicker, you also have to add the custom button in
onSelect. Furthermore, If you call$('.my-date-picker').datepicker('refresh');, the custom button goes away and there is no callback option or event to use for adding it back.Here is one of many stackoverflow.com posts on this problem.
This makes it difficult (sometimes impossible) to customize the datepicker calendar using the provided options, which leads to horrible hacks like this:
Solution
Ideally, datepicker would trigger events like
afterUpdate.datepickerand developers could then easily add event handlers to customize the behavior. However, since this isn't the current approach, I chose to add another callback option namedonUpdateDatepickerthat is always called (if defined) at the end of_updateDatepicker. This is a quick and easy solution that follows the current conventions of datepicker.The following can now be used to customize the calendar in one place:
Notes
I didn't see anything on how to update the documentation. If someone can please point me in the right direction, I will include updates for https://api.jqueryui.com/datepicker/.