I have a SfNumericTextBox like this:
<SfNumericTextBox TValue="int" Placeholder="1" FloatLabelType="@FloatLabelType.Never" @bind-value="View.Value"></SfNumericTextBox>
Every time i click into the textbox the whole number is selected.
How do i prevent the auto select of the whole textbox when i click on it.
Hi Timo Funk,
Thank you for reaching out to us. We have carefully reviewed your requirement and have created a sample based on it, utilizing the setSelectionRange method to prevent selection while focusing on the component. Below is the code snippet for your reference.
Code snippet:
|
[Index.razor]
<SfNumericTextBox CssClass="prevent-select" TValue="int" Placeholder="1" FloatLabelType="@FloatLabelType.Never" @bind-value="numValue" > <NumericTextBoxEvents TValue="int" Focus="FocusHandler"></NumericTextBoxEvents> </SfNumericTextBox>
@code{ public int numValue{get; set;} = 10;
public async void FocusHandler() { await JSRuntime.InvokeVoidAsync("preventSelectionFunction"); } }
[Layout.cshtml]
<script> function preventSelectionFunction(){ setTimeout(function () { debugger; var valLen = document.querySelectorAll('input')[0].value.length; document.querySelectorAll('input')[0].setSelectionRange(valLen, valLen); }, 100); }; </script> |
In the provided code snippet, we utilized the setSelectionRange method within a JavaScript function called preventSelectionFunction. This function is invoked when the component is focused, as specified in the FocusHandler method, we effectively prevent selection while focusing on the component. Please find the sample in the attachment.
Regards,
Yohapuja S
Hi Timo Funk,
We apologize for the delay in our response. After further investigation, we would like to inform you that the behavior you are experiencing, where the value is selected when focusing the component, is indeed the default behavior.
While we explored various possibilities to address this issue, we have determined that there is no alternative method available to achieve the desired outcome in blazor. However, if you prefer not to select the value while focusing, you can achieve this by utilizing the setSelectionRange function within the focus event using jsinterop.
To provide a more efficient solution, we have managed to reduce the time delay and successfully implemented this approach. We encourage you to review the attached sample for reference.
We appreciate your patience and understanding in this matter. If you have any further questions or concerns, please get back to us.
Regards,
Yohapuja S