1e41f4b71Sopenharmony_ci# input
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci>  **NOTE**
4e41f4b71Sopenharmony_ci>
5e41f4b71Sopenharmony_ci>  This component is supported since API version 4. Updates will be marked with a superscript to indicate their earliest API version.
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ciThe **\<input>** component provides an interactive interface to receive user input. It can be a radio button, check box, button, single-line text box, and more.
8e41f4b71Sopenharmony_ci
9e41f4b71Sopenharmony_ci## Required Permissions
10e41f4b71Sopenharmony_ci
11e41f4b71Sopenharmony_ciNone
12e41f4b71Sopenharmony_ci
13e41f4b71Sopenharmony_ci
14e41f4b71Sopenharmony_ci## Child Components
15e41f4b71Sopenharmony_ci
16e41f4b71Sopenharmony_ciNot supported
17e41f4b71Sopenharmony_ci
18e41f4b71Sopenharmony_ci
19e41f4b71Sopenharmony_ci## Attributes
20e41f4b71Sopenharmony_ci
21e41f4b71Sopenharmony_ciIn addition to the [universal attributes](js-components-common-attributes.md), the following attributes are supported.
22e41f4b71Sopenharmony_ci
23e41f4b71Sopenharmony_ci| Name                              | Type                     | Default Value      | Mandatory  | Description                                      |
24e41f4b71Sopenharmony_ci| -------------------------------- | ----------------------- | --------- | ---- | ---------------------------------------- |
25e41f4b71Sopenharmony_ci| type                             | string                  | text<br>| No   | Type of the input component. Available values include **text**, **email**, **date**, **time**, **number**, **password**, **button**, **checkbox**, and **radio**.<br>The **text**, **email**, **date**, **time**, **number**, and **password** types can be dynamically switched and modified.<br>The **button**, **checkbox**, and **radio** types cannot be dynamically modified.  <br>- **button**: a button that can be clicked.<br>- **checkbox**: a check box.<br>- **radio**: a radio button that allows users to select one from multiple others with the same name.<br>- **text**: a single-line text field.<br>- **email**: a field used for an email address.<br>- **date**: date component, including the year, month, and day, but excluding time.<br>- **time**: time component, without the time zone.<br>- **number**: field for entering digits.<br>- **password**: password field, in which characters will be shielded.|
26e41f4b71Sopenharmony_ci| checked                          | boolean                 | false     | No   | Whether the **\<input>** component is selected. This attribute is valid only when **type** is set to **checkbox** or **radio**.        |
27e41f4b71Sopenharmony_ci| name                             | string                  | -         | No   | Name of the **\<input>** component.<br>This attribute is mandatory when **type** is set to **radio**.     |
28e41f4b71Sopenharmony_ci| value                            | string                  | -         | No   | Value of the **\<input>** component. When **type** is **radio**, this attribute is mandatory and the value must be unique for radio buttons with the same name.|
29e41f4b71Sopenharmony_ci| placeholder                      | string                  | -         | No   | Content of the hint text. This attribute is available only when the component type is set to **text** \|email\|date\|time\|number\|**password**.|
30e41f4b71Sopenharmony_ci| maxlength                        | number                  | -         | No   | Maximum number of characters that can be entered in the input box. The empty value indicates no limit.          |
31e41f4b71Sopenharmony_ci| enterkeytype                     | string                  | default   | No   | Type of the **Enter** key on the soft keyboard. The value cannot be dynamically updated.<br>Available values include:<br>- default<br>- next<br>- go<br>- done<br>- send<br>- search<br>Except for the **next** type, clicking the Enter key hides the soft keyboard.|
32e41f4b71Sopenharmony_ci| headericon                       | string                  | -         | No   | Icon resource path before text input. This icon does not support click events and is unavailable for **button**, **checkbox**, and **radio** types. The supported icon image formats are JPG, PNG, and SVG.|
33e41f4b71Sopenharmony_ci| showcounter<sup>5+</sup>         | boolean                 | false     | No   | Whether to display the character counter for an input box. This attribute takes effect only when **maxlength** is set.        |
34e41f4b71Sopenharmony_ci| menuoptions<sup>5+</sup>         | Array&lt;MenuOption&gt; | -         | No   | Menu options displayed after users click the **More** button.                 |
35e41f4b71Sopenharmony_ci| autofocus<sup>6+</sup>           | boolean                 | false     | No   | Whether to automatically obtain focus.<br>This attribute setting does not take effect on the application home page. You can enable a text box on the home page to automatically obtain focus, by delaying the **focus** method call (for about 100–500 ms) in **onActive**.|
36e41f4b71Sopenharmony_ci| selectedstart<sup>6+</sup>       | number                  | -1        | No   | Start position for text selection.                          |
37e41f4b71Sopenharmony_ci| selectedend<sup>6+</sup>         | number                  | -1        | No   | End position for text selection.                          |
38e41f4b71Sopenharmony_ci| softkeyboardenabled<sup>6+</sup> | boolean                 | true      | No   | Whether to display the soft keyboard during editing.                           |
39e41f4b71Sopenharmony_ci| showpasswordicon<sup>6+</sup>    | boolean                 | true      | No   | Whether to display the icon at the end of the password text box. This attribute is available only when **type** is set to **password**.        |
40e41f4b71Sopenharmony_ci
41e41f4b71Sopenharmony_ci**Table 1** MenuOption<sup>5+</sup>
42e41f4b71Sopenharmony_ci
43e41f4b71Sopenharmony_ci| Name     | Type    | Description         |
44e41f4b71Sopenharmony_ci| ------- | ------ | ----------- |
45e41f4b71Sopenharmony_ci| icon    | string | Path of the icon for a menu option.|
46e41f4b71Sopenharmony_ci| content | string | Text content of a menu option.|
47e41f4b71Sopenharmony_ci
48e41f4b71Sopenharmony_ci
49e41f4b71Sopenharmony_ci## Styles
50e41f4b71Sopenharmony_ci
51e41f4b71Sopenharmony_ciIn addition to the [universal styles](js-components-common-styles.md), the following styles are supported.
52e41f4b71Sopenharmony_ci
53e41f4b71Sopenharmony_ci| Name                      | Type                        | Default Value       | Mandatory  | Description                                      |
54e41f4b71Sopenharmony_ci| ------------------------ | -------------------------- | ---------- | ---- | ---------------------------------------- |
55e41f4b71Sopenharmony_ci| color                    | &lt;color&gt;              | \#e6000000 | No   | Font color of the single-line text box or button.                         |
56e41f4b71Sopenharmony_ci| font-size                | &lt;length&gt;             | 16px       | No   | Font size of the single-line text box or button.                         |
57e41f4b71Sopenharmony_ci| allow-scale              | boolean                    | true       | No   | Whether the font size changes with the system's font size settings.<br>If the **config-changes** tag of **fontSize** is configured for abilities in the **config.json** file, the setting takes effect without application restart.|
58e41f4b71Sopenharmony_ci| placeholder-color        | &lt;color&gt;              | \#99000000 | No   | Color of the hint text in the single-line text box. This attribute is available only when the component type is set to **text**, **email**, **date**, **time**, **number**, or **password**. |
59e41f4b71Sopenharmony_ci| font-weight              | number \| string | normal     | No   | Font weight of the single-line text box or button. For details, see **font-weight** of the [**\<text>**](js-components-basic-text.md) component. |
60e41f4b71Sopenharmony_ci| caret-color<sup>6+</sup> | &lt;color&gt;              | -          | No   | Color of the caret.                              |
61e41f4b71Sopenharmony_ci
62e41f4b71Sopenharmony_ci
63e41f4b71Sopenharmony_ci## Events
64e41f4b71Sopenharmony_ci
65e41f4b71Sopenharmony_ciIn addition to the [universal events](js-components-common-events.md), the following events are supported.
66e41f4b71Sopenharmony_ci
67e41f4b71Sopenharmony_ci- When **type** is set to **text**, **email**, **date**, **time**, **number**, or **password**, the following events are supported.
68e41f4b71Sopenharmony_ci
69e41f4b71Sopenharmony_ci  | Name                       | Parameter                                      | Description                                      |
70e41f4b71Sopenharmony_ci  | ------------------------- | ---------------------------------------- | ---------------------------------------- |
71e41f4b71Sopenharmony_ci  | change                    | {<br> value: inputValue<br> }  | Triggered when the content entered in the input box changes. The most recent content entered by the user is returned.<br>If you change the **value** attribute directly, this event will not be triggered.|
72e41f4b71Sopenharmony_ci  | enterkeyclick             | {<br> value: enterKey<br> }    | Triggered when the **Enter** key on the soft keyboard is clicked. The type of the **Enter** key is returned, which is of the number type. Available values are as follows:<br>- **2**: returned if **enterkeytype** is **go**.<br>- **3**: returned if **enterkeytype** is **search**.<br>- **4**: returned if **enterkeytype** is **send**.<br>- **5**: returned if **enterkeytype** is **next**.<br>- **6**: returned if **enterkeytype** is **default**, **done**, or is not set.|
73e41f4b71Sopenharmony_ci  | translate<sup>5+</sup>    | {<br> value: selectedText<br> } | Triggered when users click the translate button in the menu displayed after they select a text segment. The selected text content is returned.|
74e41f4b71Sopenharmony_ci  | share<sup>5+</sup>        | {<br> value: selectedText<br> } | Triggered when users click the share button in the menu displayed after they select a text segment. The selected text content is returned.|
75e41f4b71Sopenharmony_ci  | search<sup>5+</sup>       | {<br> value: selectedText<br> } | Triggered when users click the search button in the menu displayed after they select a text segment. The selected text content is returned.|
76e41f4b71Sopenharmony_ci  | optionselect<sup>5+</sup> | {<br> index: optionIndex,<br> value: selectedText<br> } | Triggered when users click a menu option in the menu displayed after they select a text segment. This event is valid only when the **menuoptions** attribute is set. The option index and selected text content are returned.|
77e41f4b71Sopenharmony_ci  | selectchange<sup>6+</sup> | {<br>start: number,<br>end: number<br> } | Triggered when the text selection changes.                            |
78e41f4b71Sopenharmony_ci
79e41f4b71Sopenharmony_ci- When **type** is set to **checkbox** or **radio**, the following events are supported.
80e41f4b71Sopenharmony_ci
81e41f4b71Sopenharmony_ci  | Name    | Parameter                                      | Description                                      |
82e41f4b71Sopenharmony_ci  | ------ | ---------------------------------------- | ---------------------------------------- |
83e41f4b71Sopenharmony_ci  | change | {<br> checked:true \| false <br>} | Triggered when the checked status of the **checkbox** or **radio** button changes.|
84e41f4b71Sopenharmony_ci
85e41f4b71Sopenharmony_ci## Methods
86e41f4b71Sopenharmony_ci
87e41f4b71Sopenharmony_ciIn addition to the [universal methods](js-components-common-methods.md), the following methods are supported.
88e41f4b71Sopenharmony_ci
89e41f4b71Sopenharmony_ci| Name                 | Parameter                                      | Description                                      |
90e41f4b71Sopenharmony_ci| ------------------- | ---------------------------------------- | ---------------------------------------- |
91e41f4b71Sopenharmony_ci| focus               | {<br> focus: true\|false<br> }:<br>If **focus** is not passed, the default value **true** is used. | Obtains or loses focus. When **type** is set to **text**, **email**, **date**, **time**, **number**, or **password**, the input method can be displayed or collapsed. |
92e41f4b71Sopenharmony_ci| showError           | {<br> error: string <br>}                | Displays the error message. This method is available when **type** is set to **text**, **email**, **date**, **time**, **number**, or **password**. |
93e41f4b71Sopenharmony_ci| delete<sup>6+</sup> | -                                        | Deletes text based on the current caret position when **type** is set to **text**, **email**, **date**, **time**, **number**, or **password**; deletes the last character and displays the caret if the current input component does not have a caret. |
94e41f4b71Sopenharmony_ci
95e41f4b71Sopenharmony_ci## Example
96e41f4b71Sopenharmony_ci
97e41f4b71Sopenharmony_ci1. Single-line text box
98e41f4b71Sopenharmony_ci   ```html
99e41f4b71Sopenharmony_ci   <!-- xxx.hml -->
100e41f4b71Sopenharmony_ci   <div class="content">
101e41f4b71Sopenharmony_ci     <input id="input" class="input" type="text" value="" maxlength="20" enterkeytype="send"
102e41f4b71Sopenharmony_ci       headericon="/common/search.svg" placeholder="Please input text" onchange="change"
103e41f4b71Sopenharmony_ci       onenterkeyclick="enterkeyClick">
104e41f4b71Sopenharmony_ci     </input>
105e41f4b71Sopenharmony_ci     <input class="button" type="button" value="Submit" onclick="buttonClick" style="color: blue"></input>
106e41f4b71Sopenharmony_ci   </div>
107e41f4b71Sopenharmony_ci   ```
108e41f4b71Sopenharmony_ci
109e41f4b71Sopenharmony_ci   ```css
110e41f4b71Sopenharmony_ci   /* xxx.css */
111e41f4b71Sopenharmony_ci   .content {
112e41f4b71Sopenharmony_ci     width: 100%;
113e41f4b71Sopenharmony_ci     flex-direction: column;
114e41f4b71Sopenharmony_ci     align-items: center;
115e41f4b71Sopenharmony_ci   }
116e41f4b71Sopenharmony_ci   .input {
117e41f4b71Sopenharmony_ci     width: 60%;
118e41f4b71Sopenharmony_ci     placeholder-color: gray;
119e41f4b71Sopenharmony_ci   }
120e41f4b71Sopenharmony_ci   .button {
121e41f4b71Sopenharmony_ci     width: 60%;
122e41f4b71Sopenharmony_ci     background-color: gray;
123e41f4b71Sopenharmony_ci     margin-top: 20px;
124e41f4b71Sopenharmony_ci    }
125e41f4b71Sopenharmony_ci   ```
126e41f4b71Sopenharmony_ci   
127e41f4b71Sopenharmony_ci   ```js
128e41f4b71Sopenharmony_ci   // xxx.js
129e41f4b71Sopenharmony_ci   import promptAction from '@ohos.promptAction'
130e41f4b71Sopenharmony_ci   export default {
131e41f4b71Sopenharmony_ci     change(e){
132e41f4b71Sopenharmony_ci       promptAction.showToast({
133e41f4b71Sopenharmony_ci         message: "value: " + e.value,
134e41f4b71Sopenharmony_ci         duration: 3000,
135e41f4b71Sopenharmony_ci       });
136e41f4b71Sopenharmony_ci     },
137e41f4b71Sopenharmony_ci     enterkeyClick(e){
138e41f4b71Sopenharmony_ci       promptAction.showToast({
139e41f4b71Sopenharmony_ci         message: "enterkey clicked",
140e41f4b71Sopenharmony_ci         duration: 3000,
141e41f4b71Sopenharmony_ci       });
142e41f4b71Sopenharmony_ci     },
143e41f4b71Sopenharmony_ci     buttonClick(e){
144e41f4b71Sopenharmony_ci       this.$element("input").showError({
145e41f4b71Sopenharmony_ci         error: 'error text'
146e41f4b71Sopenharmony_ci       });
147e41f4b71Sopenharmony_ci     },
148e41f4b71Sopenharmony_ci    }
149e41f4b71Sopenharmony_ci   ```
150e41f4b71Sopenharmony_ci
151e41f4b71Sopenharmony_ci   ![1-2](figures/1-2.png)
152e41f4b71Sopenharmony_ci   
153e41f4b71Sopenharmony_ci2. Common button
154e41f4b71Sopenharmony_ci   ```html
155e41f4b71Sopenharmony_ci   <!-- xxx.hml -->
156e41f4b71Sopenharmony_ci   <div class="div-button">
157e41f4b71Sopenharmony_ci     <input class="button" type="button" value="Input-Button"></input>
158e41f4b71Sopenharmony_ci   </div>
159e41f4b71Sopenharmony_ci   ```
160e41f4b71Sopenharmony_ci
161e41f4b71Sopenharmony_ci   ```css
162e41f4b71Sopenharmony_ci   /* xxx.css */
163e41f4b71Sopenharmony_ci   .div-button {
164e41f4b71Sopenharmony_ci     flex-direction: column;
165e41f4b71Sopenharmony_ci     align-items: center;
166e41f4b71Sopenharmony_ci   }
167e41f4b71Sopenharmony_ci   .button {
168e41f4b71Sopenharmony_ci     margin-top: 30px;
169e41f4b71Sopenharmony_ci     width: 280px;
170e41f4b71Sopenharmony_ci   }
171e41f4b71Sopenharmony_ci   ```
172e41f4b71Sopenharmony_ci
173e41f4b71Sopenharmony_ci   ![en-us_image_0000001198898293](figures/en-us_image_0000001198898293.png)
174e41f4b71Sopenharmony_ci
175e41f4b71Sopenharmony_ci3. Check box
176e41f4b71Sopenharmony_ci   ```html
177e41f4b71Sopenharmony_ci   <!-- xxx.hml -->
178e41f4b71Sopenharmony_ci   <div class="content">
179e41f4b71Sopenharmony_ci     <input onchange="checkboxOnChange" checked="true" type="checkbox"></input>
180e41f4b71Sopenharmony_ci   </div>
181e41f4b71Sopenharmony_ci   ```
182e41f4b71Sopenharmony_ci
183e41f4b71Sopenharmony_ci   ```css
184e41f4b71Sopenharmony_ci   /* xxx.css */
185e41f4b71Sopenharmony_ci   .content{
186e41f4b71Sopenharmony_ci     width: 100%;
187e41f4b71Sopenharmony_ci     height: 200px;
188e41f4b71Sopenharmony_ci     align-items: center; 
189e41f4b71Sopenharmony_ci     justify-content: center;   
190e41f4b71Sopenharmony_ci   }
191e41f4b71Sopenharmony_ci   ```
192e41f4b71Sopenharmony_ci
193e41f4b71Sopenharmony_ci   ```js
194e41f4b71Sopenharmony_ci   // xxx.js
195e41f4b71Sopenharmony_ci   import promptAction from '@ohos.promptAction'
196e41f4b71Sopenharmony_ci   export default {
197e41f4b71Sopenharmony_ci     checkboxOnChange(e) {
198e41f4b71Sopenharmony_ci       promptAction.showToast({
199e41f4b71Sopenharmony_ci         message:'checked: ' + e.checked,
200e41f4b71Sopenharmony_ci         duration: 3000,
201e41f4b71Sopenharmony_ci       });
202e41f4b71Sopenharmony_ci     }
203e41f4b71Sopenharmony_ci   }
204e41f4b71Sopenharmony_ci   ```
205e41f4b71Sopenharmony_ci
206e41f4b71Sopenharmony_ci   ![en-us_image_0000001173324749](figures/en-us_image_0000001173324749.png)
207e41f4b71Sopenharmony_ci
208e41f4b71Sopenharmony_ci4. Radio button
209e41f4b71Sopenharmony_ci   ```html
210e41f4b71Sopenharmony_ci   <!-- xxx.hml -->
211e41f4b71Sopenharmony_ci   <div class="content">
212e41f4b71Sopenharmony_ci     <input type="radio" checked='true' name="radioSample" value="radio1" onchange="onRadioChange('radio1')"></input>
213e41f4b71Sopenharmony_ci     <input type="radio" checked='false' name="radioSample" value="radio2" onchange="onRadioChange('radio2')"></input>
214e41f4b71Sopenharmony_ci     <input type="radio" checked='false' name="radioSample" value="radio3" onchange="onRadioChange('radio3')"></input>
215e41f4b71Sopenharmony_ci   </div>
216e41f4b71Sopenharmony_ci   ```
217e41f4b71Sopenharmony_ci
218e41f4b71Sopenharmony_ci   ```css
219e41f4b71Sopenharmony_ci   /* xxx.css */
220e41f4b71Sopenharmony_ci   .content{
221e41f4b71Sopenharmony_ci     width: 100%;
222e41f4b71Sopenharmony_ci     height: 200px;
223e41f4b71Sopenharmony_ci     justify-content: center;
224e41f4b71Sopenharmony_ci     align-items: center;
225e41f4b71Sopenharmony_ci   }
226e41f4b71Sopenharmony_ci   ```
227e41f4b71Sopenharmony_ci
228e41f4b71Sopenharmony_ci   ```js
229e41f4b71Sopenharmony_ci   // xxx.js
230e41f4b71Sopenharmony_ci   import promptAction from '@ohos.promptAction'
231e41f4b71Sopenharmony_ci   export default {
232e41f4b71Sopenharmony_ci     onRadioChange(inputValue, e) {
233e41f4b71Sopenharmony_ci       if (inputValue === e.value) {
234e41f4b71Sopenharmony_ci         promptAction.showToast({
235e41f4b71Sopenharmony_ci           message: 'The chosen radio is ' + e.value,
236e41f4b71Sopenharmony_ci           duration: 3000,
237e41f4b71Sopenharmony_ci         });
238e41f4b71Sopenharmony_ci       }
239e41f4b71Sopenharmony_ci     }
240e41f4b71Sopenharmony_ci   }
241e41f4b71Sopenharmony_ci   ```
242e41f4b71Sopenharmony_ci
243e41f4b71Sopenharmony_ci   ![1-3](figures/1-3.png)
244