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.
There are 3 kinds of attributes that you can add to your HTML tags: Element-specific, global, and event handler content attributes.
The <input>
element accepts the following attributes.
Element-Specific Attributes
This table shows the attributes that are specific to the <input>
tag/element.
Attribute | Description | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
accept | Specifies a comma-separated list of content types that the server accepts.
Possible values:
You can specify both any MIME types and any corresponding extensions. For example, |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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 HTML5 elements. Therefore, you can use these attributes with the <input>
tag , as well as with all other HTML tags.
- accesskey
- class
- contenteditable
- contextmenu
- dir
- draggable
- dropzone
- hidden
- id
- inert
- itemid
- itemprop
- itemref
- itemscope
- itemtype
- lang
- spellcheck
- style
- tabindex
- title
- translate
For a full explanation of these attributes, see HTML 5 global attributes.
Event Handler Content Attributes
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.
Below are the standard HTML5 event handler content attributes.
Again, you can use any of these with the <input>
element, as well as any other HTML5 element.
- onabort
- oncancel
- onblur
- oncanplay
- oncanplaythrough
- onchange
- onclick
- oncontextmenu
- ondblclick
- ondrag
- ondragend
- ondragenter
- ondragexit
- ondragleave
- ondragover
- ondragstart
- ondrop
- ondurationchange
- onemptied
- onended
- onerror
- onfocus
- onformchange
- onforminput
- oninput
- oninvalid
- onkeydown
- onkeypress
- onkeyup
- onload
- onloadeddata
- onloadedmetadata
- onloadstart
- onmousedown
- onmousemove
- onmouseout
- onmouseover
- onmouseup
- onmousewheel
- onpause
- onplay
- onplaying
- onprogress
- onratechange
- onreadystatechange
- onscroll
- onseeked
- onseeking
- onselect
- onshow
- onstalled
- onsubmit
- onsuspend
- ontimeupdate
- onvolumechange
- onwaiting
For a full explanation of these attributes, see HTML 5 event handler content attributes.
Differences Between HTML 4 & HTML 5
The <input>
element has a number of differences between HTML 4 and HTML5.
In HTML5, the usemap
, ismap
, and align
attributes are obsolete (they were supported in HTML 4).
HTML5 introduced the following attributes:
form
autocomplete
autofocus
list
pattern
required
placeholder
dirname
formaction
formenctype
formmethod
formnovalidate
formtarget
height
max
min
minlength
multiple
step
width
Also, in HTML5 the global title
attribute has special meaning on the <input>
element.
HTML 5.1 and the WHATWG HTML Living Standard introduced the following attribute:
inputmode
But the attribute was later dropped from both specs.
To see more detail on the two versions see HTML5 <input>
Tag and HTML4 <input>
Tag. Also check out the links to the official specifications below.
Template
Here's a template for the <input>
tag with all available attributes for the tag (based on HTML5). These are grouped into attribute types, each type separated by a space. In many cases, you will probably only need one or two (if any) attributes. Simply remove the attributes you don't need.
For more information on attributes for this tag, see HTML5 <input>
Tag and HTML4 <input>
Tag.
Tag Details
For more details about the <input>
tag, see HTML5 <input>
Tag and HTML4 <input>
Tag.
Specifications
Here are the official specifications for the <input>
element.
- HTML5 Specification (W3C)
- HTML Living Standard (WHATWG)
- Current W3C Draft (the next version that is currently being worked on)
- HTML 4 (W3C)
What's the Difference?
W3C creates "snapshot" specifications that don't change once defined. So the HTML5 specification won't change once it becomes an official recommendation. WHATWG on the other hand, develops a "living standard" that is updated on a regular basis. In general, you will probably find that the HTML living standard will be more closely aligned to the current W3C draft than to the HTML5 specification.