WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit f1e4751

Browse files
kendo-botKB Bot
andauthored
Added new kb article datepicker-select-all-content-on-focus (#3408)
Co-authored-by: KB Bot <[email protected]>
1 parent e132960 commit f1e4751

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
---
2+
title: How to Select All Content in DatePicker on Focus
3+
description: Learn how to ensure the entire content of the DatePicker input is selected on focus.
4+
type: how-to
5+
page_title: Automatically Select All Text in DatePicker Input on Focus
6+
meta_title: Automatically Select All Text in DatePicker Input on Focus
7+
slug: datepicker-kb-select-all-content-on-focus
8+
tags: datepicker, focus, input, selection
9+
res_type: kb
10+
ticketid: 1704388
11+
---
12+
13+
## Environment
14+
15+
<table>
16+
<tbody>
17+
<tr>
18+
<td>Product</td>
19+
<td>DatePicker for Blazor</td>
20+
</tr>
21+
</tbody>
22+
</table>
23+
24+
## Description
25+
26+
I want the entire content of the [Telerik DatePicker](slug:components/datepicker/overview) input to be automatically selected when focused, allowing users to copy-paste dates directly.
27+
28+
## Solution
29+
30+
To ensure the entire input content is selected on focus and the popup calendar remains functional, follow these steps:
31+
32+
1. Hide the default calendar button using CSS.
33+
2. Add a custom button for opening the popup calendar.
34+
3. Utilize the [`FocusAsync`](slug:components/datepicker/overview#datepicker-reference-and-methods) and a JavaScript function to select all text in the input.
35+
36+
Here is the complete implementation:
37+
38+
````Razor
39+
@inject IJSRuntime JS
40+
41+
<style>
42+
.k-datepicker button{
43+
display: none;
44+
}
45+
</style>
46+
47+
<span @onclick="FocusAndSelect">
48+
<TelerikDatePicker @ref="DatePickerRef"
49+
@bind-Value="DatePickerValue"
50+
Width="200px" />
51+
</span>
52+
<TelerikButton Icon="@SvgIcon.Calendar" OnClick="@OpenPicker" />
53+
54+
<script>
55+
window.selectDatePickerInput = function () {
56+
// Finds the active element (the DatePicker input after FocusAsync)
57+
const input = document.activeElement;
58+
if (input && input.tagName === "INPUT") {
59+
input.select(); // selects all text
60+
}
61+
}
62+
</script>
63+
64+
@code {
65+
private TelerikDatePicker<DateTime> DatePickerRef { get; set; }
66+
private DateTime DatePickerValue { get; set; } = DateTime.Now;
67+
68+
private void OpenPicker()
69+
{
70+
DatePickerRef?.Open();
71+
}
72+
73+
private async Task FocusAndSelect()
74+
{
75+
await DatePickerRef.FocusAsync();
76+
77+
// wait a moment so the input renders focus
78+
await Task.Delay(20);
79+
80+
await JS.InvokeVoidAsync("selectDatePickerInput");
81+
}
82+
}
83+
````
84+
85+
## See Also
86+
87+
* [DatePicker Documentation](slug:components/datepicker/overview)
88+
* [FocusAsync Method](slug:components/datepicker/overview#datepicker-reference-and-methods)

0 commit comments

Comments
 (0)