W3cubDocs

/HTML

<input>

The <input> is used to create interactive controls for web-based forms in order to accept data from the user; a wide variety of types of input data and control widgets are available, depending on the device and user agent.

Content categories Flow content, listed, submittable, resettable, form-associated element, phrasing content. If the type is not hidden, then labelable element, palpable content.
Permitted content None, it is an empty element.
Tag omission Must have a start tag and must not have an end tag.
Permitted parents Any element that accepts phrasing content.
Permitted ARIA roles
DOM interface HTMLInputElement

Form <input> types

How an <input> works varies considerably depending on the value of its type attribute, hence the different types are covered in their own separate reference pages. If this attribute is not specified, the default type adopted is text.

The available types are as follows:

  • button: A push button with no default behavior.
  • checkbox: A check box allowing single values to be selected/deselected.
  • color: HTML5 A control for specifying a color. A color picker's UI has no required features other than accepting simple colors as text (more info).
  • date: HTML5 A control for entering a date (year, month, and day, with no time).
  • datetime-local: HTML5 A control for entering a date and time, with no time zone.
  • email: HTML5 A field for editing an e-mail address.
  • file: A control that lets the user select a file. Use the accept attribute to define the types of files that the control can select.
  • hidden: A control that is not displayed but whose value is submitted to the server.
  • image: A graphical submit button. You must use the src attribute to define the source of the image and the alt attribute to define alternative text. You can use the height and width attributes to define the size of the image in pixels.
  • month: HTML5 A control for entering a month and year, with no time zone.
  • number: HTML5 A control for entering a number.
  • password: A single-line text field whose value is obscured. Use the maxlength and minlength attributes to specify the maximum length of the value that can be entered.
    Note: Any forms involving sensitive information like passwords (e.g. login forms) should be served over HTTPS; Firefox now implements multiple mechanisms to warn against insecure login forms — see Insecure passwords. Other browsers are also implementing similar mechanisms.
  • radio: A radio button, allowing a single value to be selected out of multiple choices.
  • range: HTML5 A control for entering a number whose exact value is not important.
  • reset: A button that resets the contents of the form to default values.
  • search: HTML5 A single-line text field for entering search strings. Line-breaks are automatically removed from the input value.
  • submit: A button that submits the form.
  • tel: HTML5 A control for entering a telephone number.
  • text: A single-line text field. Line-breaks are automatically removed from the input value.
  • time: HTML5 A control for entering a time value with no time zone.
  • url: HTML5 A field for entering a URL.
  • week: HTML5 A control for entering a date consisting of a week-year number and a week number with no time zone.

Some input types are now obsolete:

Attributes

The <input> element is one of the most powerful and complex in all of HTML due to the sheer number of combinations of input types and attributes. Since every <input> element, regardless of type, is based on the HTMLInputElement interface, they technically all share the exact same set of attributes. However, in reality, many attributes only function on specific input types, and some input types support very few of these attributes. In addition, some input types handle certain attributes in special ways.

Here, you'll find information about the individual attributes which are common to all <input> element types, as well as a few non-standard attributes that may be worth knowing about.

Attributes common to all input types

This section lists the attributes which are used by all form <input> types. Attributes that are unique to particular input types—or attributes which are common to all input types but have special behaviors when used on a given input type—are instead documented on those types' pages.

Note: This includes the global HTML attributes.

Attribute Description
autocomplete A string indicating the type of autocomplete functionality, if any, to allow on the input
autofocus A Boolean which, if present, makes the input take focus when the form is presented
disabled A Boolean attribute which is present if the input should be disabled
form The id of the <form> of which the input is a member; if absent, the input is a member of the nearest containing form, or is not a member of a form at all
list The id of a <datalist> element that provides a list of suggested values for the input
name The input's name, to identify the input in the data submitted with the form's data
required A Boolean which, if true, indicates that the input must have a value before the form can be submitted
tabindex A numeric value providing guidance to the user agent as to the order in which controls receive focus when the user presses the Tab key
type A string indicating which input type the <input> element represents
value The input's current value

autocomplete

A string that describes what if any type of autocomplete functionality the input should provide. A typical implementation of autocomplete simply recalls previous values entered in the same input field, but more complex forms of autocomplete can exist. For instance, a browser could integrate with a device's contacts list to autocomplete email addresses in an email input field. See Values in The HTML autocomplete attribute for permitted values.

This attribute has no effect on input types that do not return numeric or text data, such as checkbox or image.

See The HTML autocomplete attribute for additional information.

autofocus

A Boolean attribute which, if present, indicates that the input should automatically have focus when the page has finished loading (or when the <dialog> containing the element has been displayed).

Note: An element with the autofocus attribute may gain focus before the DOMContentLoaded event is fired.

No more than one element in the document may have the autofocus attribute, and autofocus cannot be used on inputs of type hidden, because hidden inputs can't be focused.

Warning: Automatically focusing a form control can confuse visually-impaired people who using screen-reading technology. When autofocus is assigned, screen-readers "teleport" their user to the form control without warning them beforehand.

disabled

A Boolean attribute which, if present, indicates that the user should not be able to interact with the input. Disabled inputs are typically rendered with a dimmer color or using some other form of indication that the field is not available for use.

Specifically, disabled inputs do not receive the click event, and disabled inputs are not submitted with the form.

Note: Although not required by the specification, Firefox will by default persist the dynamic disabled state of an <input> across page loads. Use the autocomplete attribute to control this feature.

form

A string specifying the <form> element with which the input is associated (that is, its form owner). This string's value, if present, must match the id of a <form> element in the same document. If this attribute isn't specified, the <input> element is associated with the nearest containing form, if any.

The form attribute lets you place an input anywhere in the document but have it included with a form elsewhere in the document.

Note: An input can only be associated with one form.

list

The id of a <datalist> element located in the same document which provides a list of predefined values to suggest to the user for this input. Any values in the list that are not compatible with the type are not included in the suggested options.

The list attribute is not supported by the hidden, password, checkbox, radio, file, or any of the button types.

name

A string specifying a name for the input control. This name is submitted along with the control's value when the form data is submitted, as well as with the owning <form> element's elements object.

When an input element is given a name, that name becomes a property of the owning form element's HTMLFormElement.elements property. That means if you have an input whose name is set to guest and another whose name is hat-size, the following code can be used:

let form = document.querySelector("form");
let guestName = form.elements.guest;
let hatSize = form.elements["hat-size"];

When this code has run, guestName will be the HTMLInputElement for the guest field, and hatSize the object for the hat-size field.

Warning: You should avoid giving form elements a name that corresponds to a built-in property of the form, since you would then override the predefined property or method with this reference to the corresponding input.

The name _charset_ has a special meaning. If used as the name of an <input> element of type hidden, the input's value is automatically set by the user agent to the character encoding being used to submit the form.

If no name is specified, or name is empty, the input's value is not submitted with the form.

Note: For historical reasons, the name isindex is not allowed. If you really want to know why, see Naming form controls: the name attribute in the HTML specification.

required

required is a Boolean attribute which, if present, indicates that the user must specify a value for the input before the owning form can be submitted. The required attribute is supported by all input types except the following:

When an input has the required attribute, the :required pseudo-class also applies to it. Conversely, inputs without the required attribute (except the elements that don't support it) have the :optional pseudo-class applied.

Note: Because a read-only field cannot have a value, required does not have any effect on inputs with the readonly attribute also specified.

tabindex

An optional numeric value that defines both whether or not the input should be focusable through use of the Tab key as well as whether or not the element participates in sequential focus navigation. This value also establishes the order in which the element is reached using the Tab key.

The values of tabindex have special meanings depending on sign:

  • A negative value of tabindex indicates that the element should be focusable by the user, but not using sequential keyboard navigation. It's recommended to always use a value of -1 as using other values can be complicated.
  • A tabindex of 0 means that the element should be focusable and should be reachable by sequential keyboard navigation, but that the tab order is left up to the user agent, which should apply the user's platform conventions. This is usually the best value to use when you want an element to be focusable and to participate in keyboard navigation rather than trying to manage the tab order yourself.
  • A positive value of tabindex indicates the tabbing order of the element. Each time the user presses the Tab key, the element with the next sequentially higher tabindex is focused. Most platforms provide a reverse-tab feature, typically using the combination of Shift + Tab, which reverses the tabbing order.

If tabindex is omitted or is not a valid integer, the user agent follows platform conventions to determine what to do.

type

A string specifying the type of control to render. For example, to create a checkbox, a value of checkbox is used. If omitted (or an unknown value is specified), the input type text is used, creating a plaintext input field.

Permitted values are listed in Form <input> types.

value

The input control's value. When specified in the HTML, this is the initial value, and from then on it can be altered or retrieved at any time using JavaScript to access HTMLInputElement.value. The value attribute is always optional.

Note: Unlike other input controls, checkboxes and radio buttons are only included in the submitted data if the checkbox or radio button is currently checked. If it is, then the value attribute is reported as the input's value.

For example, if a checkbox whose name is status has a value of active, and the checkbox is checked, the form data submitted will include status=active. If the checkbox isn't active, it isn't listed in the form data at all. The default value for checkboxes and radio buttons is on.

Non-standard attributes common to all input types

The following attributes are not part of the HTML specification, but are available when content is loaded in some user agents. As a general rule, you should avoid these, but they are available when circumstances require that you use them, or at least require you to be aware of them.

Attribute Description
x-moz-errormessage A string to display to the user as an error message when the field doesn't pass validation. Firefox only.

x-moz-errormessage

This Mozilla-specific extension is a string which Firefox displays instead of the default error message when the field fails to validate.

Styling input elements

You can style <input> elements using various color-related attributes in particular. One unusual one that is specific to text entry-related elements is the CSS caret-color property, which lets you set the color used to draw the text input caret:

HTML

<label for="textInput">Note the red caret:</label>
<input id="textInput" class="custom" size="32">

CSS

input.custom {
  caret-color: red;
  font: 16px "Helvetica", "Arial", "sans-serif"
}

Result

For more information about adding color to elements in HTML, see Applying color to HTML elements using CSS.

Labels and placeholders

TL;DR: To save you time, here's the key point: don't use the placeholder attribute if you can avoid it. If you need to label an <input> element, use the <label> element.

There are three seemingly similar ways to associate assistive text with an <input>. However, they are actually quite different, and only one of them is always a good choice. Here we will look at each of them and learn best practices for providing the user with guidance when entering data into a form.

The <label> element

The <label> element is the only way to provide explanatory information about a form field that is always appropriate (aside from any layout concerns you have). It's never a bad idea to use a <label> to explain what should be entered into an <input> or <textarea>.

The placeholder attribute

The placeholder attribute lets you specify a text that appears within the <input> element's content area itself when empty. It's intended to be used to show an example input, rather than an explanation or prompt, but tends to be badly misused.

Here are two inputs that take a password, each with a placeholder:

Example of correct and incorrect placeholder usage

The first one uses a placeholder string MyGr8P@sswrd, demonstrating what a password might look like. And no, that's not really a great password.

The second one uses a prompt string, Enter your password as a placeholder. The first, and most obvious, problem with doing this is that as soon as the user types their first character, they no longer have a prompt explaining what that field is for.

That's why, instead, you should use the <label> element. The placeholder should never be required in order to understand your forms. While some people are able to remember what a given empty box is meant for after its only identifying text vanishes, others cannot.

If the user can't understand your form if the placeholders are missing (say, in a browser that doesn't support placeholder, or in the case above where the user starts typing then gets confused), you're not using placeholders properly.

In addition, browsers with automatic page translation features may skip over attributes when translating. That means the placeholder may not get translated, resulting in important information not being translated.

If you feel like you need to use a placeholder, it's possible to use both a placeholder and a label:

Unadorned text adjacent to the <input> element

You can also just have plain text adjacent to the <input> element, like this:

<p>Enter your name: <input id="name" type="text" size="30"></p>

Please don't do this. This doesn't create a relationship between the prompt and the <input> element, which is important for reasons we'll get into in the next section.

Why you should use labels

In addition to the information provided above, there are a number of other reasons why <label> is the best way to explain <input>s:

  • The semantic pairing of <input> and <label> elements is useful for assistive technologies such as screen readers. By pairing them using the <label>'s for attribute, you bond the label to the input in a way that lets screen readers describe inputs to users more precisely.
  • By pairing a <label> with an <input>, clicking on either one will focus the <input>. If you use plaintext to "label" your input, this won't happen. Having the prompt part of the activation area for the input is helpful for people with motor control conditions.
  • As web developers, it's important that we never assume that people will know all the things that we know. The diversity of people using the web—and by extension your web site—practically guarantees that some of your site's visitors will have some variation in thought processes and/or circumstances that leads them to interpret your forms very differently from you without clear and properly-presented labels.

Examples

You can find multiple examples of <input> element usage on the pages covering each individual type — see Form <input> types, and also see the Live example at the top of the article.

Specifications

Accessibility concerns

Labels

When including inputs, it is recommended to add labels along side. This is so those who use assistive technologies can tell what the input is for. For more information about labels in general see Labels and placeholders .

The following is an example of how to associate the <label> with an <input> element in the above style. You need to give the <input> an id attribute. The <label> then needs a for attribute whose value is the same as the input's id.

<label for="peas">Do you like peas?</label>
<input type="checkbox" name="peas" id="peas">

Browser compatibilityUpdate compatibility data on GitHub

Desktop
Chrome Edge Firefox Internet Explorer Opera Safari
type="button" 1 Yes 1 Yes Yes 1
type="checkbox" Yes Yes Yes Yes Yes Yes
type="color" 20 14 29
29
Firefox doesn't yet support inputs of type color on Windows Touch.
No 12 10
type="date" 20 12 57 No 11 No
No
The input type is recognized, but there is no date-specific control.
type="datetime-local" 20 12 No
No
See bug 888320 and TPE DOM/Date time input types.
No 11 No
No
The input type is recognized, but there is no date-specific control.
type="email" 5 ? Yes 10 11 Yes
type="file" 1 ? 1
1
You can set as well as get the value of HTMLInputElement.files in all modern browsers; this was most recently added to Firefox, in version 57 (see bug 1384030).
Yes 11 1
type="hidden" 1 Yes 1 Yes 2 1
type="image" Yes ? Yes Yes Yes Yes
type="month" 20 12 No
No
See bug 888320.
No 11 No
No
The input type is recognized, but there is no month-specific control.
type="number" Yes ? Yes 10 Yes Yes
type="password" 1 ? 1 2 2 1
type="radio" Yes Yes Yes Yes Yes Yes
type="range" 4 12 23 10 11 3.1
type="reset" 1 ? 1
1
Unlike other browsers, Firefox by default persists the dynamic disabled state of a &ltbutton&gt across page loads. Use the autocomplete attribute to control this feature.
Yes Yes 1
type="search" 5 12 4 10 10.6 5
type="submit" 1 Yes 1
1
Unlike other browsers, Firefox by default persists the dynamic disabled state of a <button> across page loads. Use the autocomplete attribute to control this feature.</button>
Yes Yes 1
type="tel" Yes Yes Yes 10 11 Yes
type="text" 1 Yes 1 Yes Yes 1
type="time" 20 12 57 No 10 No
type="url" 1 Yes Yes 10 11 Yes
type="week" 20 12 No
No
See bug 888320.
No 11 No
Mobile
Android webview Chrome for Android Edge Mobile Firefox for Android Opera for Android iOS Safari Samsung Internet
type="button" Yes 18 Yes 4 Yes Yes ?
type="checkbox" Yes Yes Yes 4 Yes Yes ?
type="color" 4.4 ? 14 27 Yes No ?
type="date" Yes Yes Yes 57 11 5 ?
type="datetime-local" Yes Yes ? Yes 11 Yes ?
type="email" ? ? ? 4 Yes 3.1
3.1
Doesn't do validation, but instead offers a custom 'email' keyboard, which is designed to make entering email addresses easier.
Automatically applies a default style of opacity: 0.4 to disable textual <input> elements, including those of type 'email'. Other major browsers don't currently share this particular default style.
?
type="file" Yes Yes ? 4 1 Yes ?
type="hidden" Yes Yes Yes 4 Yes Yes ?
type="image" Yes ? ? Yes Yes Yes ?
type="month" Yes Yes ? No Yes Yes ?
type="number" Yes Yes ? Yes Yes Yes ?
type="password" ? Yes ? 4 Yes Yes ?
type="radio" Yes Yes Yes 4 Yes Yes ?
type="range" 4.4
4.4
2 — 4.4
Android WebView recognizes the range type, but doesn't implement a range-specific control.
57 ? 52 Yes 5.1 ?
type="reset" Yes Yes ? 4
4
Unlike other browsers, Firefox by default persists the dynamic disabled state of a &ltbutton&gt across page loads. Use the autocomplete attribute to control this feature.
Yes Yes ?
type="search" Yes Yes Yes Yes Yes Yes ?
type="submit" Yes Yes Yes 4
4
Unlike other browsers, Firefox by default persists the dynamic disabled state of a <button> across page loads. Use the autocomplete attribute to control this feature.</button>
Yes Yes ?
type="tel" Yes Yes ? Yes Yes Yes ?
type="text" Yes Yes Yes 4 Yes Yes ?
type="time" Yes 25 Yes 57 Yes No ?
type="url" Yes Yes Yes Yes Yes Yes ?
type="week" Yes Yes Yes Yes Yes Yes ?

[1] It is recognized but there is no UI.

[2] Missing for type="checkbox" and type="radio".

[3] In Safari autocapitalize="words" capitalizes every word's second character.

[4] datetime has been removed from the spec and browsers in favour of datetime-local.

[5] see bug 1355389

[6] Not yet implemented. For progress, see bug 888320 and TPE DOM/Date time input types.

Notes

File inputs

  1. Starting in Gecko 2.0, calling the click() method on an <input> element of type file opens the file picker and lets the user select files. See Using files from web applications for an example and more details.

  2. You cannot set the value of a file picker from a script — doing something like the following has no effect:

    var e = getElementById("someFileInputElement");
    e.value = "foo";
    
  3. When a file is chosen using an <input type="file">, the real path to the source file is not shown in the input's value attribute for obvious security reasons. Instead, the filename is shown, with C:\fakepath\ appended to the beginning of it. There are some historical reasons for this quirk, but it is supported across all modern browsers, and in fact is defined in the spec.

Error messages

If you want Firefox to present a custom error message when a field fails to validate, you can use the x-moz-errormessage attribute to do so:

<input type="email"
 x-moz-errormessage="Please specify a valid email address.">

Note, however, that this is not standard and will not have an effect on other browsers.

Localization

The allowed inputs for certain <input> types depend on the locale. In some locales, 1,000.00 is a valid number, while in other locales the valid way to enter this number is 1.000,00.

Firefox uses the following heuristics to determine the locale to validate the user's input (at least for type="number"):

  • Try the language specified by a lang/xml:lang attribute on the element or any of its parents.
  • Try the language specified by any Content-Language HTTP header or
  • If none specified, use the browser's locale.

Using mozactionhint on Firefox mobile

You can use the mozactionhint attribute to specify the text for the label of the enter key on the virtual keyboard when your form is rendered on Firefox mobile. For example, to have a "Next" label, you can do this:

<input type="text" mozactionhint="next">

The result is:

Note the "Next" key in the lower-right corner of the keyboard.

See also

© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input