|
| 1 | +--- |
| 2 | +title: Customizing DateRangePicker Behavior in Kendo UI |
| 3 | +description: Learn how to modify the DateRangePicker control behavior to update start and end date selections accurately. |
| 4 | +type: how-to |
| 5 | +page_title: How to Customize DateRangePicker Selection Behavior |
| 6 | +slug: daterangepicker-customize-behavior |
| 7 | +tags: kendo ui, daterangepicker, custom behavior, date selection |
| 8 | +res_type: kb |
| 9 | +--- |
| 10 | + |
| 11 | +## Description |
| 12 | +When using the [DateRangePicker](https://docs.telerik.com/kendo-ui/api/javascript/ui/daterangepicker) control, selecting an end date updates the start date field instead of the end date field. The goal is to ensure that when either the start or end date is selected, the corresponding field updates accurately, providing a seamless user experience. |
| 13 | + |
| 14 | +This KB article also answers the following question: |
| 15 | +- What code changes are required to update the start and end dates correctly in DateRangePicker? |
| 16 | + |
| 17 | +## Solution |
| 18 | +To achieve the desired behavior, implement custom logic using the [`change`](https://docs.telerik.com/kendo-ui/api/javascript/ui/daterangepicker/events/change) and [`open`](https://docs.telerik.com/kendo-ui/api/javascript/ui/daterangepicker/events/open) events of the DateRangePicker. This solution involves tracking the selected start and end dates and updating them based on user interactions. |
| 19 | + |
| 20 | +1. **Initialize the DateRangePicker and define custom logic:** |
| 21 | + |
| 22 | +```javascript |
| 23 | +$(document).ready(function() { |
| 24 | + var savedRange = {}; |
| 25 | + |
| 26 | + $("#daterangepicker").kendoDateRangePicker({ |
| 27 | + change: function() { |
| 28 | + var range = this.range(); |
| 29 | + |
| 30 | + if (!savedRange.start && range.start) { |
| 31 | + savedRange.start = range.start; |
| 32 | + } |
| 33 | + |
| 34 | + if (range.start && range.end) { |
| 35 | + savedRange = range; |
| 36 | + } else if (range.start) { |
| 37 | + var newDate = range.start; |
| 38 | + |
| 39 | + if (newDate > savedRange.start) { |
| 40 | + savedRange.end = newDate; |
| 41 | + } else { |
| 42 | + savedRange = { start: newDate, end: null }; |
| 43 | + } |
| 44 | + } |
| 45 | + this.range(savedRange); |
| 46 | + }, |
| 47 | + open: function() { |
| 48 | + if (savedRange.start || savedRange.end) { |
| 49 | + this.range(savedRange); |
| 50 | + } |
| 51 | + } |
| 52 | + }); |
| 53 | +}); |
| 54 | +``` |
| 55 | +2. **Test the behavior with a dojo example:** |
| 56 | + |
| 57 | +Visit the dojo example at [https://dojo.telerik.com/NShpKjFe](https://dojo.telerik.com/NShpKjFe) to see the custom behavior in action. |
| 58 | + |
| 59 | +## See Also |
| 60 | +- [DateRangePicker Change Event Documentation](https://docs.telerik.com/kendo-ui/api/javascript/ui/daterangepicker/events/change) |
| 61 | +- [DateRangePicker Open Event Documentation](https://docs.telerik.com/kendo-ui/api/javascript/ui/daterangepicker/events/open) |
| 62 | +- [Official Kendo UI DateRangePicker Documentation](https://docs.telerik.com/kendo-ui/controls/editors/daterangepicker/overview) |
0 commit comments