HTML <input> tagS2C Home « HTML <input> tag
Definition
The <input> tag is a self closing tag that is used along with its type attribute to define an area of a form for user input.
The type attribute of the <input> element can be: button, checkbox, color, date, datetime-local, email, file, hidden, image, month, number, password, radio, range, reset, search, submit, tel, text (default), time, url or week.
Use the <label> HTML tag to give the input control a description and to aid accessibility.
Example
<p>Please fill in our Pie Survey:</p>
<form action="../htmlintermediate/simpleform.html" method="get">
<p>Name:<input type="text" name="name"></p>
<p>Which pie do you prefer?:</p>
<p><input type="radio" name="pie" checked="checked" value="Chicken" id="chicken">
<label for="chicken">Chicken</label></p>
<p><input type="radio" name="pie" value="Fish" id="fish">
<label for="fish">Fish</label></p>
<p><input type="radio" name="pie" value="Shepherds" id="shepherds">
<label for="shepherds">Shepherds</label></p>
<p>Other stuff you wanna tell us about pies:</p>
<p><textarea rows="3" cols="30" name="comments"></textarea></p>
<p>Submit your information:</p>
<p><input type="submit" value="Submit"></p>
</form>
Please fill in our Pie Survey:
Attributes
The following attributes can be used with the <input> tag.
Attribute | Value | Description | Example |
---|---|---|---|
accept | audio/* image/* video/* or MIME types without parameters or File extensions starting with a period. | Specifies a comma-separated list of content types that the server accepts. Only used when the type attribute value is file. |
|
alt | text | Specifies an alternate text for the image (provides information if the image cannot be loaded) Only used when the type attribute value is image. |
|
auto | off and on | Specifies a comma-separated list of content types that the server accepts. |
|
auto | empty string or autofocus | Specifies a field to give focus to on page load. |
|
checked | checked | Specifies a preselected state for a checkbox or radio button when the page loads. Only used when the type attribute value is checkbox or radio. |
|
dirname | text | Specifies the direction of the text entered in the input field. |
|
disabled | empty string or disabled | Specifies an input element is to be disabled until a certain condition occurs. |
|
form | form id | The form that the input element belongs to. Works with all types except type=password. |
|
form | url | Specifies the url of a file that will process the image or submission. Only used with type=image or type=submit. |
|
formenc | application/x multipart/form-data text/plain | Specifies how form-data should be encoded when sending it to a server. Only used with type=image or type=submit and method="post". |
|
form | get post | Specifies the HTTP method to be used on control submission. Only used with type=image or type=submit. |
|
formno | empty string or formno | Specifies no validation to be done on control submission. Only used with type=submit. |
|
form | Specifies where to display the response after control submission. Only used with type=image or type=submit. |
| |
_blank | Opens linked document in new window or tab. | ||
_parent | Opens linked document in parent frame. | ||
_self (default) | Opens linked document in same frame as it was clicked in. | ||
_top | Opens linked document in window body | ||
frame | Opens the linked document in a named framename. | ||
height | number | Specifies the height of an image in pixels. Only used with type=image. |
|
list | list_id | Specifies a datalist element to use for a list of predefined options where the value must equal the id of a <datalist> tag in the same document. |
|
max | number | Specifies a maximum value for an input date, number or range. Only used with type=date, type=datetime-local, type=month, type=number, type=range, type=time, or type=week. |
|
max | text | Specifies an input elements maximum character length. Only used with type=text or type=password |
|
min | number | Specifies a minimum value for an input date, number or range. Only used with type=date, type=datetime-local, type=month, type=number, type=range, type=time, or type=week. |
|
min | text | Specifies an input elements minimum character length. Only used with type=text or type=password |
|
multiple | name | Specifies the user can enter more than one value in a field. Only used with type=email or type=file |
|
name | name | Specifies a name for the input element. Allows the server or some JavaScript to identify the data. |
|
pattern | regex | Specifies a regular expression that input is validated against on form submission. Only used with type=email, type= |
|
place | regex | Specifies a reminder (word or phrase) to aid with data entry. Only used with type=email, type=password, type=search, type=tel, |
|
readonly | empty string or readonly | Specifies that an input field is protected and not modifiable. Cannot be used with type=password. |
|
required | empty string or required | Specifies an input field must be populated prior to submission. Only used with type=checkbox, type=date, type=datetime-local, type=email, type=file, type=month, type=number, type=radio, type=tel, type=text, type=time, type=url or type=week. |
|
src | button reset submit | Specifies the URL of the image (the actual location where the image is stored). Only used with type=image. |
|
size | number | Specifies the visible width of an input field. Only used with type=email, type=password, type=search, type=tel, type=text, or type=url. |
|
step | number | Specifies the granularity of a valid number. Only used with type=date, type=datetime-local, type=month, type=number, type=range, type=time, or type=week. |
|
type | button checkbox color date datetime-local email file hidden image month number password radio range reset search submit tel text time url week | Specifies the type of input element. |
|
value | text | Specifies a default value dependant on input type. works will all niput types apart from type=file. |
|
width | number | Specifies the width of an image in pixels. Only used with type=image. |
|
Attribute | Description | Example |
---|---|---|
Common | ||
class | Specifies a classname for the element allowing you to apply the style of the predefined class to the content. |
|
id | Specifies a unique id for the element allowing you to apply the style of the predefined id to the content. |
|
style | Specifies an inline style for the element allowing you to apply the style to the contentt. |
|
title | Specifies an inline style for the element allowing you to apply the style to the content. |
|
Keyboard | ||
accesskey | Specifies a keyboard shortcut to associate with the element. |
|
tabindex | Specifies a tab order for the element. |
|
Language | ||
dir | Specifies the directional flow of the content. |
|
lang | Specifies a language code for the content of the element. |
|
spell | Specifies an inline style for the element allowing you to apply the style to the contentt. |
|
translate | Specifies an inline style for the element allowing you to apply the style to the content. |
|
Miscellaneous | ||
content | Specifies whether the content of the element is editable. |
|
draggable | Specifies whether the element is draggable. |
|
hidden | Specifies whether the element is not yet, or no longer, relevant. |
|
Attribute | Description | Example |
---|---|---|
Document Element | ||
oncopy | The script to be run when the user copies the content of an element. |
|
oncut | The script to be run when the user cuts the content of an element. |
|
onpaste | The script to be run when the user pastes some content into an element. |
|
Drag and Drop | ||
ondrag | The script to be run when an element is dragged. |
|
ondragend | The script to be run when an element has stopped being dragged. |
|
ondrag | The script to be run when an element has been dragged to a valid drop target. |
|
ondrag | The script to be run when an element leaves a valid drop target. |
|
ondragover | The script to be run when an element is being dragged over a valid drop target. |
|
ondrag | The script to be run at the start of a drag operation. |
|
ondrop | The script to be run when a dragged element is being dropped. |
|
Form | ||
onblur | The script to be run when the element loses focus. |
|
onchange | The script to be run when object changed and attempt to leave field. |
|
oncontext | The script to be run when a context menu is triggered. |
|
onfocus | The script to be run when the element gets focus. |
|
oninput | The script to be run when an element gets user input. |
|
oninvalid | The script to be run when an element is invalid. |
|
onreset | The script to be run when a dragged element is being dropped. |
|
onselect | The script to be run when some or all of the contents of an object are selected. |
|
onsubmit | The script to be run when a form is submitted. |
|
Keyboard | ||
onkeydown | The script to be run when an element is in focus and keyboard key is pressed down. |
|
onkeypress | The script to be run when an element is in focus and keyboard key is pressed down and released. |
|
onkeyup | The script to be run when an element is in focus and keyboard key is released. |
|
Media | ||
onabort | The script code to be run on abort. |
|
oncanplay | The script to be run when a file has buffered enough so it is ready to start playing. |
|
oncanplay | The script to be run when a file can be played all the way to the end without further need of buffering. |
|
oncue | The script to be run when the cue changes when using the track element. |
|
onduration | The script to be run when the length of the media is changed. |
|
onemptied | The script to be run when a media resource element suddenly becomes empty, usually due to an error. |
|
onended | The script to be run when the media has reach the end. |
|
onloaded | The script to be run when media data is loaded and playback can start. |
|
onloaded | The script to be run when metadata has been loaded. |
|
onload | The script to be run whenthe media resource has started loading. |
|
onpause | The script to be run when the media resource has been paused. |
|
onplay | The script to be run when the media resource starts playback. |
|
onplaying | The script to be run when when playback has already begun. |
|
onprogress | The script to be run when the browser is fetching the media data. |
|
onrate | The script to be run when the playback rate changes. |
|
onseeked | The script to be run when the seeking attribute is set to false indicating that seeking has finished. |
|
onseeking | The script to be run when the seeking attribute is set to true indicating that seeking is currently active. |
|
onstalled | The script to be run when the browser is unable to continue fetching media data. |
|
onsuspend | The script to be run when media data has stopped before being completely loaded. |
|
ontime | The script to be run when the media resources current playback position has changed. |
|
onvolume | The script to be run when the volume has changed or been muted. |
|
onwaiting | The script to be run when the media resource has paused but is expected to resume. |
|
Mouse | ||
onclick | The script to be run when when a mouse is clicked on an element. |
|
ondblclick | The script to be run when a mouse is double clicked on an element. |
|
onmouse | The script to be run when he mouse button is pressed down while the cursor is over an element. |
|
onmouse | The script to be run when the mouse button is moved. |
|
onmouseout | The script to be run when the mouse cursor moves off an element. |
|
onmouse | The script to be run when the mouse cursor moves over an element. |
|
onmouseup | The script to be run when the mouse button is released while the cursor is over the element. |
|
onwheel | The script to be run when the mouse wheel rolls up or down over an element. |
|
Scroll | ||
onscroll | The script code to be run when the scrollbar of an element is being scrolled. |
|
Window - NONE |