About Lesson
The HTML input element supports a set of common attributes that can be used to further customize and control form inputs. Here are some commonly used form attributes for the HTML input element:
form
attribute:
Theform
attribute associates an input field with a specific form using the form’s id. This allows inputs to be outside of the form element but still associated with it.
<form id="myForm">
<input type="text" name="username" form="myForm">
</form>
required
attribute:
Therequired
attribute specifies that an input field must be filled out before submitting the form.
<input type="text" name="username" required>
readonly
attribute:
Thereadonly
attribute makes an input field read-only, preventing users from modifying its value.
<input type="text" name="username" value="JohnDoe" readonly>
disabled
attribute:
Thedisabled
attribute disables an input field, preventing user interaction and data entry.
<input type="text" name="username" value="JohnDoe" disabled>
maxlength
attribute:
Themaxlength
attribute specifies the maximum number of characters allowed in an input field.
<input type="text" name="username" maxlength="20">
min
andmax
attributes:
Themin
andmax
attributes define the minimum and maximum values for numeric input fields.
<input type="number" name="age" min="18" max="99">
step
attribute:
Thestep
attribute specifies the increment or decrement value for numeric input fields.
<input type="number" name="quantity" min="1" max="10" step="2">
autofocus
attribute:
Theautofocus
attribute automatically sets the focus on an input field when the page loads.
<input type="text" name="username" autofocus>
autocomplete
attribute:
Theautocomplete
attribute controls whether the browser should remember and provide autocomplete suggestions for an input field.
<input type="text" name="email" autocomplete="off">
These are just a few examples of form attributes that can be used with the HTML input element. There are additional attributes available depending on the specific type of input field and desired behavior. It’s important to refer to the HTML specification or relevant documentation for a comprehensive list of input attributes and their usage.
Join the conversation