html

How to Add Default Value for HTML ?

In HTML, “<textarea>” is the element that allows users to input multi-line data. Therefore, it is also known as multi-line input control. It is mostly used in forms or surveys to get descriptions or comments from the user. A text area can take any combination and length of plain text characters, integers, and symbols. It also utilizes the “name” to reference the form and the “id” attribute to link the text area to any label.

This post will explain the method for adding the default value for HTML <textarea>.

How to Add Default Value for HTML “<textarea>”?

HTML “<textarea>” element does not support any “value” attribute to set default text. Users are required to add a default value between “<textarea>” tags.

Here, we have listed down different methods for adding the default value for HTML “<textarea>”:

Method 1: Setting Default Value by Adding Text in <textarea>

To set the default text in the HTML “<textarea>” element, go through the provided instructions.

Step 1: Create Container
First, create a div container with the help of the “<div>” element. Then, add an “id” attribute inside the div opening tag.

Step 2: Add <“textarea”> Element
Next, create a “<textarea>” element to create a text area for adding text to the container. After that, insert the “name” attribute to specify the name of an element and embed text in between the “<textarea>” opening and closing tag:

<div id="txt-area">
<textarea name='text-field'>Enter Your Text Here by Replacing it</textarea>
</div>

It can be noticed that the text area with the default value has been created:

Step 3: Style “div” Container
Now, utilize the “#txt-area” for accessing the id attribute in the “div” container, where“#” is an “id” selector. Then, apply the following CSS properties:

#txt-area{
 border: 3px double blue;
 margin: 70px;
 padding: 20px;
 background-color: rgb(149, 238, 149);
 text-align: center;
}

Here:

  • The “border” property is used for defining a boundary outside of the element.
  • margin” defines a space outside of the defined boundary.
  • padding” specifies the space inside of the defined border.
  • background-color” property is utilized for setting the color at the element’s backside.
  • text-align” property sets the alignment of the text in an element.

Output

Method 2: Setting Default Value by Defining the Length of Rows and Columns

Utilize the “<textarea>” tag for creating a text area. Then, insert the following attribute inside the “<textarea>” tag:

  • class” attribute specifies one or more class names for an element.
  • rows” adds the rows inside the element according to the defined value.
  • type” specifies the type of the elements:
<textarea class="input" rows="10" type="text">Default Value</textarea>

Output

Next, if you want to style the text area, access the class by using the class name and “.” selector, and apply the CSS properties as per your requirements.

Method 3: Setting Default Value by Using the “onfocus” Attribute

Users can also add the default value by using the “onfocus” attribute inside the “<textarea>” opening tag. The “onfocus” attribute is utilized for making the text disappear when the user clicks on the text area to enter the text:

<textarea name="message" rows = "5" cols=text areafocus="this.innerHTML=''">Insert Your Message Here</textarea>

Output

Method 4: Setting Default Value by Using the “placeholder” Attribute

You can also add the default value with the help of the “placeholder”. When a user clicks the text field to enter the text, the added default value will disappear:

<textarea placeholder="Enter Your Data Here"></textarea>

Output

That was all about adding the default value for HTML “<textarea>”.

Conclusion

To add a default value for HTML “<textarea>”, users can either include text in between the “<textarea>” tags, define the length of rows and columns, utilize the “onfocus” attribute, or “placeholder” attribute. This post has demonstrated the methods for adding the default value for HTML “<textarea>” element.

About the author

Hafsa Javed