Web Platform Atlas
EN⚡RUUA⚡DE⚡FR⚡ES⚡PL⚡PT⚡ZH⚡JA⚡
Каталог / HTML / The input element

4.10.5 The input element

Статический предпросмотр (RU): для MDN, WPT, глоссария и BYOK-перевода откройте интерактивный Атлас.
⚡ Открыть главу в Атласе

4.10.5 The input element

Категории:

потоковое содержимое.

Фразовое содержимое.

If the type attribute is not in the Hidden state: Interactive content.

If the type attribute is not in the Hidden state: Listed, labelable, submittable, resettable, and autocapitalize-and-autocorrect inheriting form-associated element.

If the type attribute is in the Hidden state: Listed, submittable, resettable, and autocapitalize-and-autocorrect inheriting form-associated element.

If the type attribute is not in the Hidden state: Palpable content.

Контексты, в которых может использоваться этот элемент:

Где ожидается Фразовое содержимое.

Контентная модель:

Nothing.

Опущение тегов в text/html:

No end tag.

Атрибуты контента:

глобальные атрибуты

accept — Hint for expected file type in file upload controls

alpha — Allow the color's alpha component to be set

alt — Replacement text for use when images are not available

autocomplete — Hint for form autofill feature

checked — Whether the control is checked

colorspace — The color space of the serialized color

dirname — Name of form control to use for sending the element's directionality in form submission

disabled — Whether the form control is disabled

form — Associates the element with a form element

formaction — URL to use for form submission

formenctype — Entry list encoding type to use for form submission

formmethod — Variant to use for form submission

formnovalidate — Bypass form control validation for form submission

formtarget — Navigable for form submission

height — Vertical dimension

list — List of autocomplete options

max — Maximum value

maxlength — Maximum length of value

min — Minimum value

minlength — Minimum length of value

multiple — Whether to allow multiple values

name — Name of the element to use for form submission and in the form.elements API

pattern — Pattern to be matched by the form control's value

placeholder — User-visible label to be placed within the form control

popovertarget — Targets a popover element to toggle, show, or hide

popovertargetaction — Indicates whether a targeted popover element is to be toggled, shown, or hidden

readonly — Whether to allow the value to be edited by the user

required — Whether the control is required for form submission

size — Size of the control

src — Адрес ресурса

step — Granularity to be matched by the form control's value

switch — Whether the control is a switch

type — Type of form control

value — Value of the form control

width — Horizontal dimension

Also, the title attribute has special semantics on this element: Description of pattern (when used with pattern attribute)

Специфика доступности:

type attribute in the Hidden state: for authors; for implementers.

type attribute in the Text state: for authors; for implementers.

type attribute in the Search state: for authors; for implementers.

type attribute in the Telephone state: for authors; for implementers.

type attribute in the URL state: for authors; for implementers.

type attribute in the Email state: for authors; for implementers.

type attribute in the Password state: for authors; for implementers.

type attribute in the Date state: for authors; for implementers.

type attribute in the Month state: for authors; for implementers.

type attribute in the Week state: for authors; for implementers.

type attribute in the Time state: for authors; for implementers.

type attribute in the Local Date and Time state: for authors; for implementers.

type attribute in the Number state: for authors; for implementers.

type attribute in the Range state: for authors; for implementers.

type attribute in the Color state: for authors; for implementers.

type attribute in the Checkbox state, without the switch attribute: for authors; for implementers.

type attribute in the Checkbox state, with the switch attribute: for authors; for implementers.

type attribute in the Radio Button state: for authors; for implementers.

type attribute in the File Upload state: for authors; for implementers.

type attribute in the Submit Button state: for authors; for implementers.

type attribute in the Image Button state: for authors; for implementers.

type attribute in the Reset Button state: for authors; for implementers.

type attribute in the Button state: for authors; for implementers.

Санитизация:

Uncategorized with navigating URL attributes formaction.

DOM-интерфейс:

[Exposed=Window] interface HTMLInputElement : HTMLElement { [HTMLConstructor] constructor(); [CEReactions, Reflect] attribute DOMString accept; [CEReactions, Reflect] attribute boolean alpha; [CEReactions, Reflect] attribute DOMString alt; [CEReactions, ReflectSetter] attribute DOMString autocomplete; [CEReactions, Reflect="checked"] attribute boolean defaultChecked; attribute boolean checked; [CEReactions] attribute DOMString colorSpace; [CEReactions, Reflect] attribute DOMString dirName; [CEReactions, Reflect] attribute boolean disabled; readonly attribute HTMLFormElement? form; attribute FileList? files; [CEReactions, ReflectSetter] attribute USVString formAction; [CEReactions] attribute DOMString formEnctype; [CEReactions] attribute DOMString formMethod; [CEReactions, Reflect] attribute boolean formNoValidate; [CEReactions, Reflect] attribute DOMString formTarget; [CEReactions, ReflectSetter] attribute unsigned long height; attribute boolean indeterminate; readonly attribute HTMLDataListElement? list; [CEReactions, Reflect] attribute DOMString max; [CEReactions, ReflectNonNegative] attribute long maxLength; [CEReactions, Reflect] attribute DOMString min; [CEReactions, ReflectNonNegative] attribute long minLength; [CEReactions, Reflect] attribute boolean multiple; [CEReactions, Reflect] attribute DOMString name; [CEReactions, Reflect] attribute DOMString pattern; [CEReactions, Reflect] attribute DOMString placeholder; [CEReactions, Reflect] attribute boolean readOnly; [CEReactions, Reflect] attribute boolean required; [CEReactions, Reflect] attribute unsigned long size; [CEReactions, ReflectURL] attribute USVString src; [CEReactions, Reflect] attribute DOMString step; [CEReactions, Reflect] attribute boolean switch; [CEReactions] attribute DOMString type; [CEReactions, Reflect="value"] attribute DOMString defaultValue; [CEReactions] attribute [LegacyNullToEmptyString] DOMString value; attribute object? valueAsDate; attribute unrestricted double valueAsNumber; [CEReactions, ReflectSetter] attribute unsigned long width; undefined stepUp(optional long n = 1); undefined stepDown(optional long n = 1); readonly attribute boolean willValidate; readonly attribute ValidityState validity; readonly attribute DOMString validationMessage; boolean checkValidity(); boolean reportValidity(); undefined setCustomValidity(DOMString error); readonly attribute NodeList? labels; undefined select(); attribute unsigned long? selectionStart; attribute unsigned long? selectionEnd; attribute DOMString? selectionDirection; undefined setRangeText(DOMString replacement); undefined setRangeText(DOMString replacement, unsigned long start, unsigned long end, optional SelectionMode selectionMode = "preserve"); undefined setSelectionRange(unsigned long start, unsigned long end, optional DOMString direction); undefined showPicker(); // also has obsolete members }; HTMLInputElement includes PopoverTargetAttributes;

The input element represents a typed data field, usually with a form control to allow the user to edit the data.

The type attribute controls the data type (and associated control) of the element. It is an enumerated attribute with the following keywords and states:

Keyword

State

Data type

Control type

hidden

Hidden

An arbitrary string

n/a

text

текст

Text with no line breaks

A text control

search

Search

Text with no line breaks

Search control

tel

Telephone

Text with no line breaks

A text control

url

url

An absolute URL

A text control

email

Email

An email address or list of email addresses

A text control

password

Пароль

Text with no line breaks (sensitive information)

A text control that obscures data entry

date

дата

A date (year, month, day) with no time zone

A date control

month

месяц

A date consisting of a year and a month with no time zone

A month control

week

Week

A date consisting of a week-year number and a week number with no time zone

A week control

time

Time

A time (hour, minute, seconds, fractional seconds) with no time zone

A time control

datetime-local

Local Date and Time

A date and time (year, month, day, hour, minute, second, fraction of a second) with no time zone

A date and time control

number

Number

A numerical value

A text control or spinner control

range

Range

A numerical value, with the extra semantic that the exact value is not important

A slider control or similar

color

Color

An sRGB color with 8-bit red, green, and blue components

A color picker

checkbox

Checkbox

A set of zero or more values from a predefined list

A checkbox or switch

radio

Radio Button

An enumerated value

A radio button

file

File Upload

Zero or more files each with a MIME type and optionally a filename

A label and a button

submit

Submit Button

An enumerated value, with the extra semantic that it must be the last value selected and initiates form submission

A button

image

Image Button

A coordinate, relative to a particular image's size, with the extra semantic that it must be the last value selected and initiates form submission

Either a clickable image, or a button

reset

Reset Button

n/a

A button

button

Button

n/a

A button

The attribute's missing value default and invalid value default are both the Text state.

Which of the accept, alpha, alt, autocomplete, checked, colorspace, dirname, formaction, formenctype, formmethod, formnovalidate, formtarget, height, list, max, maxlength, min, minlength, multiple, pattern, placeholder, readonly, required, size, src, step, switch, and width content attributes, the checked, files, valueAsDate, valueAsNumber, and list IDL attributes, the select() method, the selectionStart, selectionEnd, and selectionDirection, IDL attributes, the setRangeText() and setSelectionRange() methods, the stepUp() and stepDown() methods, and the input and change events apply to an input element depends on the state of its type attribute. The subsections that define each type also clearly define in normative "bookkeeping" sections which of these feature apply, and which do not apply, to each type. The behavior of these features depends on whether they apply or not, as defined in their various sections (q.v. for content attributes, for APIs, for events).

The following table is non-normative and summarizes which of those content attributes, IDL attributes, methods, and events apply to each state:

Hidden

Text, Search

Telephone, URL

Email

Password

Date, Month, Week, Time

Local Date and Time

Number

Range

Color

Checkbox

Radio Button

File Upload

Submit Button

Image Button

Reset Button, Button

Content attributes

accept

·

·

·

·

·

·

·

·

·

·

·

·

Да

·

·

·

alpha

·

·

·

·

·

·

·

·

·

Да

·

·

·

·

·

·

alt

·

·

·

·

·

·

·

·

·

·

·

·

·

·

Да

·

autocomplete

Да

Да

Да

Да

Да

Да

Да

Да

Да

Да

·

·

·

·

·

·

checked

·

·

·

·

·

·

·

·

·

·

Да

Да

·

·

·

·

colorspace

·

·

·

·

·

·

·

·

·

Да

·

·

·

·

·

·

dirname

Да

Да

Да

Да

Да

·

·

·

·

·

·

·

·

Да

·

·

formaction

·

·

·

·

·

·

·

·

·

·

·

·

·

Да

Да

·

formenctype

·

·

·

·

·

·

·

·

·

·

·

·

·

Да

Да

·

formmethod

·

·

·

·

·

·

·

·

·

·

·

·

·

Да

Да

·

formnovalidate

·

·

·

·

·

·

·

·

·

·

·

·

·

Да

Да

·

formtarget

·

·

·

·

·

·

·

·

·

·

·

·

·

Да

Да

·

height

·

·

·

·

·

·

·

·

·

·

·

·

·

·

Да

·

list

·

Да

Да

Да

·

Да

Да

Да

Да

Да

·

·

·

·

·

·

max

·

·

·

·

·

Да

Да

Да

Да

·

·

·

·

·

·

·

maxlength

·

Да

Да

Да

Да

·

·

·

·

·

·

·

·

·

·

·

min

·

·

·

·

·

Да

Да

Да

Да

·

·

·

·

·

·

·

minlength

·

Да

Да

Да

Да

·

·

·

·

·

·

·

·

·

·

·

multiple

·

·

·

Да

·

·

·

·

·

·

·

·

Да

·

·

·

pattern

·

Да

Да

Да

Да

·

·

·

·

·

·

·

·

·

·

·

placeholder

·

Да

Да

Да

Да

·

·

Да

·

·

·

·

·

·

·

·

popovertarget

·

·

·

·

·

·

·

·

·

·

·

·

·

Да

Да

Да

popovertargetaction

·

·

·

·

·

·

·

·

·

·

·

·

·

Да

Да

Да

readonly

·

Да

Да

Да

Да

Да

Да

Да

·

·

·

·

·

·

·

·

required

·

Да

Да

Да

Да

Да

Да

Да

·

·

Да

Да

Да

·

·

·

size

·

Да

Да

Да

Да

·

·

·

·

·

·

·

·

·

·

·

src

·

·

·

·

·

·

·

·

·

·

·

·

·

·

Да

·

step

·

·

·

·

·

Да

Да

Да

Да

·

·

·

·

·

·

·

switch

·

·

·

·

·

·

·

·

·

·

Да

·

·

·

·

·

width

·

·

·

·

·

·

·

·

·

·

·

·

·

·

Да

·

IDL attributes and methods

checked

·

·

·

·

·

·

·

·

·

·

Да

Да

·

·

·

·

files

·

·

·

·

·

·

·

·

·

·

·

·

Да

·

·

·

value

default

value

value

value

value

value

value

value

value

value

default/on

default/on

filename

default

default

default

valueAsDate

·

·

·

·

·

Да

·

·

·

·

·

·

·

·

·

·

valueAsNumber

·

·

·

·

·

Да

Да

Да

Да

·

·

·

·

·

·

·

list

·

Да

Да

Да

·

Да

Да

Да

Да

Да

·

·

·

·

·

·

select()

·

Да

Да

Yes†

Да

Yes†

Yes†

Yes†

·

Yes†

·

·

Yes†

·

·

·

selectionStart

·

Да

Да

·

Да

·

·

·

·

·

·

·

·

·

·

·

selectionEnd

·

Да

Да

·

Да

·

·

·

·

·

·

·

·

·

·

·

selectionDirection

·

Да

Да

·

Да

·

·

·

·

·

·

·

·

·

·

·

setRangeText()

·

Да

Да

·

Да

·

·

·

·

·

·

·

·

·

·

·

setSelectionRange()

·

Да

Да

·

Да

·

·

·

·

·

·

·

·

·

·

·

stepDown()

·

·

·

·

·

Да

Да

Да

Да

·

·

·

·

·

·

·

stepUp()

·

·

·

·

·

Да

Да

Да

Да

·

·

·

·

·

·

·

Events

input event

·

Да

Да

Да

Да

Да

Да

Да

Да

Да

Да

Да

Да

·

·

·

change event

·

Да

Да

Да

Да

Да

Да

Да

Да

Да

Да

Да

Да

·

·

·

† If the control has no selectable text, the select() method results in a no-op, with no "InvalidStateError" DOMException.

Some states of the type attribute define a value sanitization algorithm.

Each input element has a value, which is exposed by the value IDL attribute. Some states define an algorithm to convert a string to a number, an algorithm to convert a number to a string, an algorithm to convert a string to a Date object, and an algorithm to convert a Date object to a string, which are used by max, min, step, valueAsDate, valueAsNumber, and stepUp().

An input element's dirty value flag must be set to true whenever the user interacts with the control in a way that changes the value. (It is also set to true when the value is programmatically changed, as described in the definition of the value IDL attribute.)

The value content attribute gives the default value of the input element.

Each input element has a checkedness, which is exposed by the checked IDL attribute.

Each input element has a boolean dirty checkedness flag. When it is true, the element is said to have a dirty checkedness. The dirty checkedness flag must be initially set to false when the element is created, and must be set to true whenever the user interacts with the control in a way that changes the checkedness.

The checked content attribute is a boolean attribute that gives the default checkedness of the input element.

The input HTML element attribute change steps, given element, localName, oldValue, and value, are:

If localName is value and element's dirty value flag is false:

Set element's value to value if it is not null; otherwise the empty string.

Run element's current value sanitization algorithm, if one is defined.

If localName is checked and element's dirty checkedness flag is false:

If oldValue is null and value is not null, then set element's checkedness to true.

Otherwise, if oldValue is not null and value is null, then set element's checkedness to false.

If localName is type:

Let oldState and newState be the type attribute's states corresponding to oldValue and value, respectively.

If oldState is not newState, then update an input element's type given element, oldState, and newState.

If localName is multiple and element's type attribute is in the Email state, then run element's value sanitization algorithm.

Если все следующие условия истинны:

localName is alpha or colorspace;

oldValue is not value; and

element's type attribute is in the Color state,

then run update a color well control color given element.

The reset algorithm for input elements is to set its user validity, dirty value flag, and dirty checkedness flag back to false, set the value of the element to the value of the value content attribute, if there is one, or the empty string otherwise, set the checkedness of the element to true if the element has a checked content attribute and false if it does not, empty the list of selected files, and then invoke the value sanitization algorithm, if the type attribute's current state defines one.

Each input element can be mutable. Except where otherwise specified, an input element is always mutable. Similarly, except where otherwise specified, the user agent should not allow the user to modify the element's value or checkedness.

When an input element is disabled, it is not mutable.

The readonly attribute can also in some cases (e.g. for the Date state, but not the Checkbox state) stop an input element from being mutable.

The input element can support a picker. A picker is a user interface element that allows the end user to choose a value. Whether an input element supports a picker depends on the type attribute state and implementation-defined behavior. An input element must support a picker when its type attribute is in the File Upload state.

As of the time of this writing, typical browser implementations show such picker UI for:

input elements whose type attributes are in the Date, Month, Week, Time, Local Date and Time, and Color states;

input elements in various states that have a suggestions source element;

input elements whose type attribute is in the File Upload state; and

select elements.

The cloning steps for input elements given node, copy, and subtree are to propagate the value, dirty value flag, checkedness, dirty checkedness flag, and indeterminateness from node to copy.

The activation behavior for input elements element, given event, are these steps:

If element is not mutable, and element's type attribute is neither in the Checkbox nor in the Radio state, then return.

Run element's input activation behavior, if any, and do nothing otherwise.

If element has a form owner and element's type attribute is not in the Button state, then return.

Run the popover target attribute activation behavior given element and event's target.

Recall that an element's activation behavior runs for both user-initiated activations and for synthetic activations (e.g., via el.click()). User agents might also have behaviors for a given control — not specified here — that are triggered only by true user-initiated activations. A common choice is to show the picker, if applicable, for the control. In contrast, the input activation behavior only shows pickers for the special historical cases of the File Upload and Color states.

The legacy-pre-activation behavior for input elements are these steps:

If this element's type attribute is in the Checkbox state, then set this element's checkedness to its opposite value (i.e. true if it is false, false if it is true) and set this element's indeterminateness to false.

If this element's type attribute is in the Radio Button state, then get a reference to the element in this element's radio button group that has its checkedness set to true, if any, and then set this element's checkedness to true.

The legacy-canceled-activation behavior for input elements are these steps:

If the element's type attribute is in the Checkbox state, then set the element's checkedness and the element's indeterminateness back to the values they had before the legacy-pre-activation behavior was run.

If this element's type attribute is in the Radio Button state, then if the element to which a reference was obtained in the legacy-pre-activation behavior, if any, is still in what is now this element's radio button group, if it still has one, and if so, set that element's checkedness to true; or else, if there was no such element, or that element is no longer in this element's radio button group, or if this element no longer has a radio button group, set this element's checkedness to false.

When an input element is first created, the element's rendering and behavior must be set to the rendering and behavior defined for the type attribute's state, and the value sanitization algorithm, if one is defined for the type attribute's state, must be invoked.

To update an input element's type given an input element element, its previous type state oldState, and its new type state newState:

If oldState put the value IDL attribute in the value mode, element's value is not the empty string, and newState puts the value IDL attribute in either the default mode or the default/on mode, then set element's value content attribute to element's value.

Otherwise, if oldState put the value IDL attribute in any mode other than the value mode, and newState puts the value IDL attribute in the value mode, then set the value of element to the value of its value content attribute, if there is one, or the empty string otherwise, and then set element's dirty value flag to false.

Otherwise, if oldState put the value IDL attribute in any mode other than the filename mode, and newState puts the value IDL attribute in the filename mode, then set the value of element to the empty string.

Update element's rendering and behavior to newState's.

Signal a type change for element. (The Radio Button state uses this, in particular.)

Invoke the value sanitization algorithm for element, if one is defined for newState.

Let previouslySelectable be true if setRangeText() previously applied to element in oldState, and false otherwise.

Let nowSelectable be true if setRangeText() now applies to element in newState, and false otherwise.

If previouslySelectable is false and nowSelectable is true, then set element's text entry cursor position to the beginning of the text control, and set its selection direction to "none".

The name attribute represents the element's name. The dirname attribute controls how the element's directionality is submitted. The disabled attribute is used to make the control non-interactive and to prevent its value from being submitted. The form attribute is used to explicitly associate the input element with its form owner. The autocomplete attribute controls how the user agent provides autofill behavior.

Each input element has an indeterminateness, a boolean which is initially false. It only has an effect on checkbox controls whose switch attribute is not set, including their rendering and accessibility semantics. An input element's indeterminateness is independent of its checkedness.

The indeterminate getter steps are to return this's indeterminateness.

The indeterminate setter steps are to set this's indeterminateness to the given value.

The colorSpace IDL attribute must reflect the colorspace content attribute, limited to only known values. The type IDL attribute must reflect the respective content attribute of the same name, limited to only known values.

The width and height getter steps are to return the respective component of the result of determining the dimensions of this.

To determine the dimensions of an input element input:

If input's type attribute is not in the Image Button state, then return a width of 0 and a height of 0. (In that case no image is available.)

Let naturalDimensions be null.

If an image is available, then set naturalDimensions to the result of applying the default sizing algorithm with that image's natural width, natural height, and natural aspect ratio, if any, using a default object size of 300 by 150.

Return the result of determining the dimensions of input given naturalDimensions.

The willValidate, validity, and validationMessage IDL attributes, and the checkValidity(), reportValidity(), and setCustomValidity() methods, are part of the constraint validation API. The labels IDL attribute provides a list of the element's labels. The select(), selectionStart, selectionEnd, selectionDirection, setRangeText(), and setSelectionRange() methods and IDL attributes expose the element's text selection. The disabled, form, and name IDL attributes are part of the element's forms API.