|
| 1 | +--- |
| 2 | +title: Clearing ComboBox Value With Backspace in Kendo UI |
| 3 | +description: Learn how to clear a ComboBox value using the backspace key without automatically selecting the first item in the list when filtering is applied. |
| 4 | +type: how-to |
| 5 | +page_title: How to Clear ComboBox Value Using Backspace Without Auto-Selecting First Item in Kendo UI |
| 6 | +slug: clear-combobox-value-backspace-kendo-ui |
| 7 | +tags: kendo-ui, combobox, clear, value, backspace, filter |
| 8 | +res_type: kb |
| 9 | +ticketid: 1665651 |
| 10 | +--- |
| 11 | + |
| 12 | +## Environment |
| 13 | + |
| 14 | +<table> |
| 15 | +<tbody> |
| 16 | +<tr> |
| 17 | +<td>Product</td> |
| 18 | +<td>ComboBox for Progress® Kendo UI®</td> |
| 19 | +</tr> |
| 20 | +<tr> |
| 21 | +<td>Version</td> |
| 22 | +<td>2024.3.806</td> |
| 23 | +</tr> |
| 24 | +</tbody> |
| 25 | +</table> |
| 26 | + |
| 27 | +## Description |
| 28 | + |
| 29 | +When I set the `filter: 'contains'` option in a [ComboBox](https://docs.telerik.com/kendo-ui/api/javascript/ui/combobox), and I try to clear the selected value using the backspace key, the focus moves to the first item in the dropdown list. If I press the Enter key at this point, the first item is automatically selected, but I want the ComboBox to be cleared and remain empty instead, similar to when the filter option is not set. How can I achieve this behavior? |
| 30 | + |
| 31 | +This KB article also answers the following questions: |
| 32 | +- How to prevent automatic selection of the first item in Kendo UI ComboBox after clearing the value? |
| 33 | + |
| 34 | +## Solution |
| 35 | + |
| 36 | +To ensure that the ComboBox does not automatically select the first item when you clear the input and press the Enter key, you can handle the `keydown` event. When the BackSpace key is pressed you can check the length of the current value of the ComboBox and set a flag based on it. Next, you can check if the Enter key is pressed and if the input field of the ComboBox is empty. If both conditions are met, you can set the value of the ComboBox to empty. |
| 37 | + |
| 38 | +Here is how you can implement this: |
| 39 | + |
| 40 | +```javascript |
| 41 | + |
| 42 | +var combo = $("#combobox").data('kendoComboBox'); |
| 43 | +var isEmpty = false; |
| 44 | + |
| 45 | +combo.input.on("keydown", function (e) { |
| 46 | + if(e.keyCode === kendo.keys.BACKSPACE && combo.text().length <= 1 ) { |
| 47 | + |
| 48 | + isEmpty = true; |
| 49 | + } |
| 50 | + |
| 51 | + if(e.keyCode === kendo.keys.ENTER && isEmpty) { |
| 52 | + combo.value('') |
| 53 | + isEmpty = false |
| 54 | + } |
| 55 | + }); |
| 56 | +``` |
| 57 | + |
| 58 | +Below you will find a runnable example: |
| 59 | + |
| 60 | +```dojo |
| 61 | +<input type="text" id="combobox" /> |
| 62 | +<script> |
| 63 | +
|
| 64 | + $(document).ready(function() { |
| 65 | + var dataSource = new kendo.data.DataSource({ |
| 66 | + data: [ |
| 67 | + { Id: 1, sportName: "Basketball"}, |
| 68 | + { Id: 2, sportName: "Golf"}, |
| 69 | + { Id: 3, sportName: "Baseball"}, |
| 70 | + { Id: 4, sportName: "Table Tennis"}, |
| 71 | + { Id: 5, sportName: "Volleyball"}, |
| 72 | + { Id: 6, sportName: "Football"}, |
| 73 | + { Id: 7, sportName: "Boxing"}, |
| 74 | + { Id: 8, sportName: "Badminton"}, |
| 75 | + { Id: 9, sportName: "Cycling"}, |
| 76 | + { Id: 10, sportName: "Gymnastics"}, |
| 77 | + { Id: 11, sportName: "Swimming"}, |
| 78 | + { Id: 12, sportName: "Wrestling"}, |
| 79 | + { Id: 13, sportName: "Snooker"}, |
| 80 | + { Id: 14, sportName: "Skiing"}, |
| 81 | + { Id: 15, sportName: "Handball"} |
| 82 | + ] |
| 83 | + }); |
| 84 | +
|
| 85 | + $("#combobox").kendoComboBox({ |
| 86 | + dataTextField: "sportName", |
| 87 | + dataValueField: "Id", |
| 88 | + filter: 'contains', |
| 89 | + dataSource: dataSource, |
| 90 | + placeholder: "Please select your favourite sport..." |
| 91 | + }); |
| 92 | +
|
| 93 | + var combo = $("#combobox").data('kendoComboBox'); |
| 94 | + var isEmpty = false |
| 95 | +
|
| 96 | + combo.input.on("keydown", function (e) { |
| 97 | + if(e.keyCode === kendo.keys.BACKSPACE && combo.text().length <= 1 ) { |
| 98 | +
|
| 99 | + isEmpty = true; |
| 100 | + } |
| 101 | +
|
| 102 | + if(e.keyCode === kendo.keys.ENTER && isEmpty) { |
| 103 | + combo.value('') |
| 104 | + isEmpty = false |
| 105 | + } |
| 106 | + }); |
| 107 | +
|
| 108 | + }); |
| 109 | +</script> |
| 110 | +``` |
| 111 | + |
| 112 | +## See Also |
| 113 | + |
| 114 | +- [ComboBox API Reference](https://docs.telerik.com/kendo-ui/api/javascript/ui/combobox) |
| 115 | +- [ComboBox Keyboard Navigation (Demo)](https://demos.telerik.com/kendo-ui/combobox/keyboard-navigation) |
0 commit comments