HTML - Input Attributes
HTML - Input Attributes
The HTML <input> tag provides different attributes to define its characteristics and behavior.
Input Attributes
The HTML input attributes define the characteristics and behavior of the <input> element. These input attributes are used with the different types of input fields, such as text, email, password, date, number, and so forth. Note that the input element is used to create interactive controls for the web-based forms so that it can accept data from the user.
The <input> element requires only an opening tag, and it will work only if we add it in between the <form> tags. In this tutorial, we are going to explore the attributes that are used with the <input> element.
The attributes of the <input> element are as follows β
-
type and name
-
value
-
size
-
maxlength
-
readonly
-
disabled
-
min and max
-
accept and multiple
-
placeholder
-
required
-
autofocus
-
list
The βtypeβ and βnameβ Attributes
The type attribute indicates the type of input control, like text, password, email, and so on. The name attribute of an input element assigns an identifier to the form control that enables the server to recognize and retrieve the value.
Example
The following HTML code illustrates the use of type and name attributes:
The βvalueβ Attribute
The value attribute is used to provide an initial value inside the input control.
Example
In the following example, we are creating two input fields with initial value as β first nameβ¦β and β last nameβ¦β:
The βsizeβ Attribute
The size attribute allows you to specify the width of the text-input control in terms of characters. The default size is 20 characters.
Example
In this example, the size of the text-input control is set to 40:
The βmaxlengthβ Attribute
The maxlength attribute allows you to specify the maximum number of characters a user can enter into the text box.
Example
The following example demonstrates how to set the maxlength of an input field:
The βreadonlyβ Attribute
The readonly attribute of an input field indicates the field as read-only. Although the content of a read-only field cannot be altered, users can still select it and copy the text. Also, the value of a read-only field is included when the form is submitted.
Example
The following example shows the use of the readonly attribute of the <input> element:
The βdisabledβ Attribute
The disabled attribute of an input field indicates the field as disabled. Unlike readonly, the value of a disabled field will not be included when the form is submitted.
Example
In this example, the field containing the organization name is marked as disabled:
The βminβ and βmaxβ Attributes
The min and max attributes determine the minimum and maximum values, respectively, of an input field like number, date, week, and so on. If we use them together, they will allow users to enter an input within a predefined range.
Example
In the following example, we are mentioning the minimum working hours as 3 and maximum as 8 by using the min and max attributes:
The βacceptβ and βmultipleβ Attributes
The accept attribute specifies the types of files that the server will take in. If we use the multiple attribute, it will allow the users to upload more than one file.
Example
The following HTML code can accept multiple image files:
The βplaceholderβ Attribute
The placeholder attribute of an input field, like text, search, and email, briefly outlines the desired value of the field. Its predefined value is displayed in the input field until the user begins to enter their own value.
Example
In the following example, we are using the placeholder attribute for the email input field:
The βrequiredβ Attribute
The required attribute in an input field like text, search, password, and email signifies that the field must contain some values for the form to be successfully submitted. In other words, it indicates the mandatory field.
Example
The following example illustrates the use of the required attribute. Without filling the mandatory fields, users will not be able to submit the form:
The βautofocusβ Attribute
The autofocus attribute in an input field ensures that the field must be selected automatically once the webpage loads completely. It implies that the cursor will be positioned to the specified input field. In cases where multiple elements use the autofocus attribute, only the first element will acquire the focus.
Example
Following is the example of the autofocus attribute:
The βlistβ Attribute
The list attribute defines a set of predefined options for an <input> element, which are defined within a <datalist> element . The <input> element uses a specific string as an ID to create a link to the corresponding <datalist> element.
Example
In this example, we are creating a list of cities with the help of the list attribute: