The text area in HTML5 is used to create a multi-line text input control, allowing users to enter large amounts of text, such as messages or comments. Unlike the <input type=”text”> element, which is for single-line input, <textarea> is designed for longer text entries.
Characteristics and Attributes:
- Not an Empty Element: The <textarea> element is not an empty element; it requires both an opening <textarea> tag and a closing </textarea> tag.
- Initial Content: Any text placed between the opening and closing tags will appear in the text area when the page loads.
- name Attribute: Similar to other form controls, the name attribute is essential. It identifies the <textarea> element to the server when the form data is submitted.
- rows and cols Attributes: The rows attribute specifies the visible height of the text area in terms of the number of lines, and the cols attribute defines the visible width in terms of the number of characters per line. In HTML5, these attributes are no longer required, and CSS can be used to define the width and height.
- placeholder Attribute: Introduced in HTML5, the placeholder attribute provides a hint to the user about what kind of information should be entered in the field. This text disappears when the user starts typing.
- required Attribute: This HTML5 attribute specifies that the user must provide a value in the <textarea> before the form can be submitted.
- disabled Attribute: The disabled attribute, when present, makes the text area uneditable and unselectable.
- readonly Attribute: The readonly attribute makes the text area’s content uneditable, but it can still be selected and copied.
- maxlength Attribute: This attribute specifies the maximum number of characters that the user can enter into the text area.
- form Attribute: New in HTML5, the form attribute allows a <textarea> element to be associated with a <form> that it is not a descendant of. The value of this attribute should be the id of the relevant <form> element.
- autofocus Attribute: Another HTML5 addition, autofocus specifies that the text area should automatically receive focus when the page loads.
- wrap Attribute: This attribute controls how line breaks are handled when the form is submitted. Values can include soft (default, submits text without extra line breaks), hard (inserts line breaks so no line exceeds the cols value), and browser-specific values like physical (like hard) and virtual (like soft).
- dirname Attribute: This HTML5 attribute specifies that the text direction of the <textarea> element should be submitted along with the element’s value.
<form action="/submit" method="post">
<label for="comment">Enter your comment:</label><br>
<textarea id="comment" name="comment" rows="5" cols="30" placeholder="Write your thoughts here..." required maxlength="500">
This is some initial text.
</textarea><br><br>
<input type="submit" value="Submit Comment">
</form>
In this example, a <textarea> is created with 5 rows and 30 columns, displaying “Write your thoughts here…” as placeholder text. It is a required field and has a maximum character limit of 500. The initial text “This is some initial text.” will be present when the page loads. The name attribute is set to “comment”, which will be used when the form data is sent to the server.
HTML5 Topics
- <img> Tag and Attributes
- <figure> and <figcaption>
- <tr>,<td>,<th>,<tbody>,<thead>,<tfoot>, cplspan, rowspan and scope
- <form>, Action, Method-GET and POST
- Input types: Text, Password, CheckBox, radio, submit, Reset and Button
- New input Types:: Email, Url, Number, Range, Date, Time, Month, Week and color
- <label> Element