Definition and Usage
The <textarea> tag represents a multi-line text input field.
It is useful for allowing users to easily enter multiple lines of plain text, such as comments, reviews, simple notes, or source code.
Basic Example
<div style="display:flex;">
<label for="user-comment" style="margin-right: 0.5em;">Comment</label>
<textarea id="user-comment" rows="6" cols="22"></textarea>
</div>
Specifying an Initial Value
While <input type="text"> specifies its initial value using the HTML value attribute,
the <textarea> tag does not support the HTML value attribute.
Note!
The <textarea> tag does not support the HTML value attribute.
However, in JavaScript, there is a difference in that there is a defaultValue property that can be used to get or set the default value, and a value property that can be used to get or set the current value.
To specify an initial value in a <textarea> tag, insert it directly as its content.
<textarea rows="6" cols="22">
An initial value can be specified by inserting it directly as the content.
</textarea>
Using Related Attributes
Let's learn about the attributes related to <textarea>.
rows and cols Attributes
The cols attribute specifies the width (in average character widths) of the control as a positive integer. If not specified, the default value is 20.
The rows attribute specifies the number of lines of text for the control as a positive integer. If not specified, the default value is 2.
<textarea rows="6" cols="22"></textarea>
placeholder Attribute
The placeholder attribute specifies the text displayed in a text input control when no value is set. This is called placeholder text.
This value should provide the user with a brief hint about the expected type of data to enter into the control.
When the user starts entering text, the placeholder text disappears from the text input field.
<div style="display: flex;">
<label for="user-comment" style="margin-right: 0.5em;">Comment</label>
<textarea
id="user-comment"
rows="6"
cols="22"
placeholder="Enter a brief comment..."></textarea>
</div>
For information on styling placeholder text, see Styling CSS Placeholder Text.
Accessibility Considerations
The placeholder attribute cannot replace the label of the control.
placeholder attribute cannot replace the label of the control.
<textarea placeholder="Comment"></textarea>
For more information, see Accessibility Considerations for the <input> Tag.
readonly and disabled Attributes
The readonly and disabled attributes prevent the control from being changed or edited.
However, there are a few differences between these two attributes.
The readonly attribute specifies that a control is read-only. Although its content cannot be edited, the control can receive focus, and its value is submitted with the form.
The disabled attribute specifies that a control is disabled. Its content cannot be edited, it cannot receive focus, and its value is not submitted with the form.
Both of these attributes are Boolean attributes. You write only the attribute itself without specifying a value.
<textarea rows="6" cols="22" readonly>
readonly textarea
</textarea>
<br>
<textarea rows="6" cols="22" disabled>
disabled textarea
</textarea>
minlength and maxlength Attributes
The minlength attribute specifies the minimum string length (number of characters) that a user can enter. When the minlength attribute is specified for an element, the user must enter at least the specified number of characters.
The maxlength attribute specifies the maximum string length (number of characters) that a user can enter. When the maxlength attribute is specified for an element, it prevents the user from entering more than the allowed number of characters.
<textarea
rows="6"
cols="22"
minlength="10"
maxlength="20"
placeholder="Enter between 10 and 20 characters..."></textarea>
required Attribute
The required attribute indicates that the control is required, meaning that the user must enter a value before submitting the form. It is a Boolean attribute, meaning you only need to include the attribute name without specifying a value.
<form>
<div style="display: flex; margin-bottom: 1em;">
<label for="user-comment" style="margin-right: 0.5em;">Comment (*Required)</label>
<textarea
id="user-comment"
rows="6"
cols="22"
placeholder="Enter a brief comment..."
required></textarea>
</div>
<button type="submit">Submit</button>
</form>
You can see that the browser displays an error message indicating that the field is required.
name Attribute
The name attribute specifies the name of the element.
When the name attribute is specified, its value is used as the name of the data sent when the form is submitted. In JavaScript, the value of the name attribute is also used as the name of the element in the form.elements API.
form Attribute
Associating the element with a specific <form> in this way makes it a member of that <form>.
In other words, regardless of its physical location in the document, it functions as if it were inside the associated <form>. When the form attribute is included on a <textarea>, the name set on that element can be referenced by name in the associated form's form.elements API. As a result, the element can be included in the associated form data; when the form is submitted, the element's name is submitted as the data key, and its entered text content is submitted as the value.
Submitting Values on Form Submission
When a form is submitted, the name specified on the <textarea> is submitted as the data name, and the currently entered text is submitted as its value.
Note!
The <textarea> tag does not support the HTML value attribute.
However, in JavaScript, there is a difference in that there is a defaultValue property that can be used to get or set the default value, and a value property that can be used to get or set the current value.
Limitations of <textarea> and a Brief Mention of the contenteditable Attribute
Limitations of <textarea>
The <textarea> tag is designed to accept multiline text input from users. It supports only plain text, meaning users cannot directly enter or apply formatting such as bold text, italics, or links. As a result, it has limitations when implementing complex text editing features.
contenteditable Attribute
Using the contenteditable attribute allows content to be edited even in elements other than a <textarea> tag, such as a <div> tag. Naturally, it also supports entering formatted rich text, including bold text, italics, and links. Additionally, it is useful when implementing editors for drafting emails or writing blog posts.
The contenteditable attribute specifies whether the content of an element is editable.
When set to true, the content of the element can be edited. When set to false, the content cannot be edited.
<div contenteditable="true" style="padding: 15px; background: #eee; border-radius: 5px;">
Try editing this content directly.
</div>
Summary Comparison
| Feature | <textarea> |
contenteditable Attribute |
|---|---|---|
| Rich Text Formatting | Not supported | Supported |
| Data Submission | Easily submitted with forms | Requires additional processing |
| Ease of Use | Simple | Complex |
| Flexibility | Limited | High |
Technical Summary
Browser Compatibility
| Tag and Attribute |
Desktop Chrome
|
Desktop Edge
|
Desktop Firefox
|
Safari
|
|---|---|---|---|---|
<textarea>
|
1 | 12 | 1 | 4 |
rows
|
1 | 12 | 1 | 4 |
cols
|
1 | 12 | 1 | 4 |
placeholder
|
4 | 12 | 1 | 4 |
readonly
|
1 | 12 | 1 | 4 |
disabled
|
1 | 12 | 1 | 4 |
minlength
|
40 | 17 | 51 | 10.1 |
maxlength
|
4 | 12 | 4 | 5 |
required
|
4 | 12 | 4 | 5 |
form
|
1 | 12 | 1 | 4 |
Specifications
| Specification | |
|---|---|
<textarea>
|
HTML Standard #the-textarea-element |