HTML <input> Tag
The HTML <input> tag represents a form input control in an HTML document. A form input control is a control that allows the user to input data and interact with a website or application.
Syntax
The <input> tag is written as <input> (no end tag). An <input> tag is typically given a type attribute to specify the type of control, and a name attribute so that the form processor can make a reference to it. Often a value attribute is used to specify the default value of the form control.
There are a lot of other attributes that can be used with the <input> tag. These are listed below under "Template".
Here's a basic example of the syntax for the <input> tag with some attributes:
Here's an example that contains three <input> elements within a typical form:
Examples
Basic Form
Here's a form that is used to collect the user's name details. Three <input> tags are used: two for the name details, one for the submit button.
The form Attribute
It is possible to associate the <input> element with a form by using the <input> element's form attribute.
If using the <input> element's form attribute, you must specify the id of the form you wish to associate the element with.
In the following example, I've placed all the form-associated elements outside of the <input> element. But I've purposely associated only two elements with the form (to demonstrate the effect of the form attribute). The first name has been associated with a form but the last name has not. Therefore, the last name is not submitted with the form and the action page doesn't acknowledge the users' last name.
Radio Buttons
In this example, we create radio buttons. We do this by giving the type attribute a value of radio. Each radio button in a group must have the same name value - that's how they are associated. The value is what distinguishes the different options.
Form-Associated Elements
The <input> tag is referred to as a form associated element. This is because it can have a form-owner.
Here are the form-associated elements in HTML.
Attributes
Attributes can be added to an HTML element to provide more information about how the element should appear or behave.
The <input> element accepts the following attributes.
| Attribute | Description | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
accept | Specifies a comma-separated list of content types that the server accepts.
Possible values:
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
alt | Alternate text. This specifies text to be used in case the browser/user agent can't render the input control. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
autocomplete | Prevents the browser from using "autocomplete" with this control. Can be useful for sensitive data, such as banking details etc.
Possible values:
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
autofocus | Automatically gives focus to this control when the page loads. This allows the user to start using the control without having to select it first. There must not be more than one element in the document with the autofocus attribute specified.
This is a boolean attribute. If the attribute is present, its value must either be the empty string or a value that is an ASCII case-insensitive match for the attribute's canonical name, with no leading or trailing whitespace (i.e. either Possible values:
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
checked | The input control will already be selected when the page loads. Used with type="radio" or type="checkbox". |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
disabled | Disables the input control. The control won't accept changes from the user. It also cannot receive focus and will be skipped when tabbing.
This is a boolean attribute. If the attribute is present, its value must either be the empty string or a value that is an ASCII case-insensitive match for the attribute's canonical name, with no leading or trailing whitespace (i.e. either Possible values:
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
dirname | Determines the direction of the text as submitted in the input field. The value of this attribute can be a string of text such as a name for the field. For example, if you specify the attribute as dirname="text_dir", once the form has been submitted, the data might look like this: text_dir=ltr.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
form | Specifies the ID of a form to which this control belongs.
Possible values: [The ID of a form element in the element's owner |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
formaction | Specifies the URL of the file that will process the control when submitted. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
formenctype | Specifies the content type used to encode the form data set when it's submitted to the server.
Possible values:
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
formmethod | Specifies the HTTP method to use when the control is submitted.
Possible values:
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
formnovalidate | Specifies that the form is not to be validated during submission.
This is a boolean attribute. If the attribute is present, its value must either be the empty string or a value that is an ASCII case-insensitive match for the attribute's canonical name, with no leading or trailing whitespace (i.e. either Possible values:
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
formtarget | Specifies the browsing context to load the destination indicated in the action attribute.
Possible values:
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
height | Specifies the height of the control. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
list | Specifies a datalist element to use for a list of predefined options for the user. The value must be the ID of a datalist element in the same document. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
max | Specifies the maximum value for the control. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
maxlength | Specifies the maximum number of characters that the user is allowed to enter into the text field. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
min | Specifies the minimum value for the control. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
minlength | Specifies a lower bound on the number of characters a user can input. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
multiple | Specifies whether the user is allowed to enter more than one value.
This is a boolean attribute. If the attribute is present, its value must either be the empty string or a value that is an ASCII case-insensitive match for the attribute's canonical name, with no leading or trailing whitespace (i.e. either Possible values:
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
name | Assigns a name to the input control. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
pattern | Specifies a regular expression against which the control's value is to be checked. Value must match the Pattern production of ECMA 262's grammar. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
placeholder | Specifies a short hint (a word or short phrase) intended to aid the user with data entry. A hint could be a sample value or a brief description of the expected format.
Note: For a longer hint or other advisory text, the title global attribute is more appropriate. Also note that the |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
readonly | Sets the input control to read-only - it won't allow the user to change the value. The control however, can receive focus and are included when tabbing through the form controls.
This is a boolean attribute. If the attribute is present, its value must either be the empty string or a value that is an ASCII case-insensitive match for the attribute's canonical name, with no leading or trailing whitespace (i.e. either Possible values:
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
required | Specifies that the input field is a required field (the user must complete this field).
This is a boolean attribute. If the attribute is present, its value must either be the empty string or a value that is an ASCII case-insensitive match for the attribute's canonical name, with no leading or trailing whitespace (i.e. either Possible values:
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
size | Specifies the width of the control, in characters. Value that is a valid non-negative integer greater than zero. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
src | If type="image", this attribute specifies the location of the image. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
step | Specifies the granularity that is expected (and required) of the value, by limiting the allowed values. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
type | Specifies the type of control.
Possible values:
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
value | Specifies a default value for the control. Note: If type="checkbox" or type="radio" this attribute is required. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
width | Specifies the width of the control. |
Global Attributes
The following attributes are standard across all HTML elements. Therefore, you can use these attributes with the <input> tag , as well as with all other HTML tags.
accesskeyautocapitalizeclasscontenteditabledata-*dirdraggablehiddenidinputmodeisitemiditempropitemrefitemscopeitemtypelangpartslotspellcheckstyletabindextitletranslate
For a full explanation of these attributes, see HTML 5 global attributes.
Event Handlers
Event handler content attributes enable you to invoke a script from within your HTML. The script is invoked when a certain "event" occurs. Each event handler content attribute deals with a different event.
onabortonauxclickonbluroncanceloncanplayoncanplaythroughonchangeonclickoncloseoncontextmenuoncopyoncuechangeoncutondblclickondragondragendondragenterondragexitondragleaveondragoverondragstartondropondurationchangeonemptiedonendedonerroronfocusonformdataoninputoninvalidonkeydownonkeypressonkeyuponlanguagechangeonloadonloadeddataonloadedmetadataonloadstartonmousedownonmouseenteronmouseleaveonmousemoveonmouseoutonmouseoveronmouseuponpasteonpauseonplayonplayingonprogressonratechangeonresetonresizeonscrollonsecuritypolicyviolationonseekedonseekingonselectonslotchangeonstalledonsubmitonsuspendontimeupdateontoggleonvolumechangeonwaitingonwheel
Most event handler content attributes can be used on all HTML elements, but some event handlers have specific rules around when they can be used and which elements they are applicable to.
For more detail, see HTML event handler content attributes.