Introduction to TextArea Tag in HTML
The <textarea> tag is one of the many tags that HTML has. The <textarea> tag format is divided into three main parts, an opening tag (<textarea>), the content and the closing tag (</textarea>). This HTML tag is also used within a form where it is used to declare a text area element, where a user can add text over multiple rows. A <textarea> tag creates a text area that can hold a lot of characters.
How <textarea> Tag works in HTML?
When used, a textarea element creates an area or space that is specified using attributes like cols or rows or both. The look and feel can also be formatted using CSS styling, of course, using height and width properties.
<textarea rows="3" cols="20"> Enter your text here... </textarea>
The <textarea> tag, like all other HTML tags, accepts a number of other attributes which are also common in <input> form element. They are listed below:
- autofocus: The autofocus attribute makes sure that the text area automatically gets the focus when the page loads.
- cols: The ‘cols’ attribute specify the width of the text area. The value should be a positive integer. If not specified, the default value of ‘cols’ is 20.
- disabled: This feature simply disables the text area; that is, it freezes the area and does not accept any user input changes. When disabled, the text area will not receive focus and is skipped when using tabbing.
- form: The ‘form’ attribute specifies the form id to which the text area belongs.
- name: It assigns a name to the text control.
- placeholder: This attribute is used to help the user, as what is to be written in the text box, a hint, a sample text.
- readonly: This attribute sets the text area in read-only mode; that is, it is not affected by the user’s input or cannot change its content.
- wrap: This feature specifies how the text area will wrap the text. If not specified, the default value is ‘soft’.
Examples of TextArea Tags in HTML
To understand the work of text area element more, check out the following example that has <textarea> tag in action,
<form> <p>Leave your Comment:</p> <br /> <textarea id="ta" cols="60" rows="5"> Write Here...</textarea></form>
The above example is a simple one, demonstrating features of <textarea> element. For example, the ‘id’ attribute is for accessibility purposes. Another interesting feature used in this example is the use of cols and rows attributes.
The rows and cols allow the programmer to set the boundary values for the size of the text area, the exact space the text area will acquire. Using these attributes helps in cross-browser support and format consistency since browser defaults can be different.
<!DOCTYPE html> <html> <head> <title> Textarea HTML Tag Demo </title> </head> <body> <form> <p>Fill the Detail:</p> <br /> <textarea rows="5" cols="40" name="demo" maxlength="60" minlength="10" required="required">Enter your name</textarea> <br /> <input type="submit" name="Submit" value="Submit" /> </form> </body> </html>
The above example shows another set of properties being used alongside <textarea> tag, max length, and minlength. The maxlength specifies the maximum number of characters that a user can enter in the text area. So here, the user can enter ‘60’ characters, special characters included.
The max length attribute was added in HTML5; this attribute was not supported in HTML. Similarly, minlength is the property that specifies the minimum number of characters to be inserted into the text area, which here is ‘10’. Another attribute that has been used here is ‘required’, specifying that the text area will not be submitted and is invalid if it is empty. It’s a simple validation for the tag.
<form id="Form1"> <label>Textarea Box 1</label> <br /> <textarea rows="5" cols="40" name="demo" maxlength="60" minlength="10" disabled="disabled"> This is Disabled</textarea> <br /> <label>Textarea Box 2</label> <br /> <textarea rows="5" cols="40" required="required"></textarea> <br /> <label>Textarea Box 3</label> <br /> <textarea rows="5" cols="40" placeholder="This is readonly textarea" readonly="readonly"></textarea> <br /> <input type="submit" name="Submit" value="Submit" /> </form>
Observe that the ‘Textarea Box 2’ text area is a required text area, whereas the ‘Textarea Box 1’ is disabled.
<form id="label2" action="textareaDemo.html"> <fieldset> <legend><b>Form 2</b></legend> <input type="text" name="FN" value="Name" /> <br /> <input type="submit" name="Submit" value="Submit" /> <br /> </fieldset> </form> <textarea rows="5" cols="40" form="label2" required="required"></textarea> <br /> <p>Above Text Area belongs to 'Form 2'</p>
Note the output below. The textarea box below is a ‘required’ field, and as mentioned in the code above, this field is associated with the form, ‘Form 2’. Thus when we try to submit the form with an empty text area, it shows an alert.
The <textarea> element can be a nested element within a <form> tag or can exist outside a form tag but use the ‘form’ attribute to associate itself to a form. One important note is, this element does not have a ‘value’ attribute, so if you need a default text for your text area, enter that text between the opening and the closing <textarea> tags.
This is a guide to textarea Tag in HTML. Here we discuss the introduction, how <textarea> Tag works? attributes and examples, respectively. You may also have a look at the following articles to learn more –