HTML - Input Attributes



The HTML tag provides different attributes to define its characteristics and behavior.

Input Attributes

The HTML input attributes define the characteristics and behavior of the 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 element requires only an opening tag, and it will work only if we add it in between the

tags. In this tutorial, we are going to explore the attributes that are used with the element.

The attributes of the element are as follows −

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 type and name Attributes


   
      First name: 
      

Last name:

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 value Attribute
	

   
First name:

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 size Attribute


   
First name:

Last name:

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 maxlength Attribute


   
First name:

Last name:

Contact:

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 element:




   The readonly Attribute


   
Emp. Name:

Emp. Email:

Organization:

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 disabled Attribute


   
Emp. Name:

Emp. Email:

Organization:

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 min and max Attribute


   
Emp. Name:

Emp. Email:

Organization:

Working Hrs:

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 accept and multiple Attributes


   

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 placeholder Attribute


   
Emp. Name:

Emp. Email:

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 required Attribute


   

The * Star represents mandatory field

Emp. Name: *

Emp. Email: *

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 autofocus Attribute


   
Emp. Name:

Emp. Email:

The 'list' Attribute

The list attribute defines a set of predefined options for an element, which are defined within a element. The element uses a specific string as an ID to create a link to the corresponding element.

Example

In this example, we are creating a list of cities with the help of the list attribute:




   The list Attribute


   
Emp. Name:

Emp. Email:

Location −
Advertisements