HTML TextArea defaultValue Property: Setting the Default Text
The HTML <textarea> element is used to create multi-line text input areas in web forms. The defaultValue property in JavaScript allows you to set or retrieve the initial content of a <textarea> element. This is different from the value property, which reflects the current content that the user can change.
What is the defaultValue Property?
The defaultValue property specifies the initial value of a <textarea> field when the page loads. It’s particularly useful for providing default instructions, sample text, or pre-filled data in a form.
Syntax
Getting the Default Value
let defaultText = textAreaElement.defaultValue;
Setting the Default Value
textAreaElement.defaultValue = "Your default text here";
Usage Notes
- Initial Content:
defaultValuesets the text that appears when the page is first loaded. - User Changes: Modifying
defaultValuein JavaScript does not change the text currently displayed in the<textarea>element. - Resetting the Field: The
defaultValueis used when a form is reset.
Practical Examples
Let’s explore how to use the defaultValue property with some practical examples.
Example 1: Setting a Default Value on Page Load
In this example, we set the defaultValue of a <textarea> element when the page loads.
<textarea id="defaultTextArea1" rows="4" cols="50"></textarea>
<script>
const textArea1 = document.getElementById("defaultTextArea1");
textArea1.defaultValue = "Enter your comments here...";
</script>
This code will render a <textarea> field with the default text “Enter your comments here…”.
Example 2: Retrieving the Default Value
This example demonstrates how to retrieve the default value of a <textarea> element using JavaScript.
<textarea id="defaultTextArea2" rows="4" cols="50">
This is the initial default value.
</textarea>
<p id="defaultValueDisplay"></p>
<script>
const textArea2 = document.getElementById("defaultTextArea2");
const displayParagraph = document.getElementById("defaultValueDisplay");
const defaultValue = textArea2.defaultValue;
displayParagraph.textContent = "Default Value: " + defaultValue;
</script>
Here, the script retrieves the defaultValue and displays it in a paragraph element.
Example 3: Using defaultValue in a Form
This example shows how the defaultValue is used when a form is reset.
<form id="myForm">
<textarea id="defaultTextArea3" rows="4" cols="50">
Initial default text.
</textarea>
<br />
<button type="reset">Reset Form</button>
</form>
<script>
const form = document.getElementById("myForm");
form.addEventListener("reset", () => {
const textArea3 = document.getElementById("defaultTextArea3");
console.log("TextArea value after reset:", textArea3.value);
});
</script>
When the “Reset Form” button is clicked, the <textarea> will revert to its defaultValue.
Example 4: Dynamically Setting defaultValue
In this example, we dynamically set the defaultValue of a <textarea> element based on a user interaction.
<textarea id="defaultTextArea4" rows="4" cols="50"></textarea>
<br />
<button id="setDefaultButton">Set Default Text</button>
<script>
const textArea4 = document.getElementById("defaultTextArea4");
const setDefaultButton = document.getElementById("setDefaultButton");
setDefaultButton.addEventListener("click", () => {
textArea4.defaultValue = "This text was set dynamically!";
});
</script>
Clicking the “Set Default Text” button will set the defaultValue of the <textarea> element. Note that this does not change the current value of the textarea, only the underlying default.
Example 5: Comparing value and defaultValue
This example illustrates the difference between the value and defaultValue properties.
<textarea id="defaultTextArea5" rows="4" cols="50">
Initial Value
</textarea>
<br />
<button id="showValuesButton">Show Values</button>
<p id="valuesDisplay"></p>
<script>
const textArea5 = document.getElementById("defaultTextArea5");
const showValuesButton = document.getElementById("showValuesButton");
const valuesDisplay = document.getElementById("valuesDisplay");
showValuesButton.addEventListener("click", () => {
const currentValue = textArea5.value;
const defaultValue = textArea5.defaultValue;
valuesDisplay.textContent =
"Current Value: " + currentValue + ", Default Value: " + defaultValue;
});
</script>
This code will display the current value (which can be modified by the user) and the default value of the <textarea> element.
Browser Support
The defaultValue property is supported by all major browsers, including:
- Chrome
- Firefox
- Safari
- Edge
- Opera
Tips and Best Practices
- Use for Initial Hints: Use
defaultValueto provide initial instructions or sample text to guide users. - Distinguish from
value: Remember thatdefaultValueis for the initial state, whilevaluereflects the current state. - Form Reset Behavior: Keep in mind that the
defaultValueis used when a form is reset.
Conclusion
The defaultValue property of the HTML <textarea> element is a useful tool for setting the initial content of a text area. Understanding how to use this property can help you create more user-friendly and functional web forms. By providing default text, you can guide users and ensure that forms are properly filled out.








