HTMLTextAreaElement
Baseline Widely available *
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
* Some parts of this feature may have varying levels of support.
TheHTMLTextAreaElement interface provides properties and methods for manipulating the layout and presentation of<textarea> elements.
In this article
Instance properties
Also inherits properties from its parent interface,HTMLElement.
autocompleteA string that represents the element's
autocompleteattribute.colsA number that represents the element's
colsattribute, indicating the visible width of the text area.defaultValueA string that represents the control's default value, which behaves like the
Node.textContentproperty.dirNameA string that represents the directionality of the element.
disabledA boolean that represents the element's
disabledattribute, indicating that the control is not available for interaction.formRead onlyReturns a reference to the parent form element. If this element is not contained in a form element, it can be the
idattribute of any<form>element in the same document or the valuenull.labelsRead onlyReturns a
NodeListof the<label>elements associated with this element.maxLengthA number that represents the element's
maxlengthattribute, indicating the maximum number of characters the user can enter. This constraint is evaluated only when the value changes.minLengthA number that represents the element's
minlengthattribute, indicating the minimum number of characters the user can enter. This constraint is evaluated only when the value changes.nameA string that represents the element's
nameattribute, containing the name of the control.placeholderA string that represents the element's
placeholderattribute, containing a hint to the user about what to enter in the control.readOnlyA boolean that represents the element's
readonlyattribute, indicating that the user cannot modify the value of the control.requiredA boolean that represents the element's
requiredattribute, indicating that the user must specify a value before submitting the form.rowsA number that represents the element's
rowsattribute, indicating the number of visible text lines for the control.selectionDirectionA string that represents the direction in which selection occurred. This is
forwardif selection was performed in the start-to-end direction of the current locale, orbackwardfor the opposite direction. This can also benoneif the direction is unknown.selectionEndA number that represents the index of the end of selected text. If no text is selected, it contains the index of the character that follows the input cursor. On being set, the control behaves as if
setSelectionRange()had been called with this as the second argument, andselectionStartas the first argument.selectionStartA number that represents the index of the beginning of selected text. If no text is selected, it contains the index of the character that follows the input cursor. On being set, the control behaves as if
setSelectionRange()had been called with this as the first argument andselectionEndas the second argument.textLengthRead onlyReturns the code point length of the control's
value. Same as readingvalue.length.typeRead onlyReturns the string
textarea.validationMessageRead onlyReturns a localized message that describes the validation constraints that the control does not satisfy (if any). This is the empty string if the control is not a candidate for constraint validation (
willValidateisfalse), or it satisfies its constraints.validityRead onlyReturns the validity state that this element is in.
valueA string that represents the raw value contained in the control.
willValidateRead onlyReturns whether the element is a candidate for constraint validation.
falseif any conditions bar it from constraint validation, including itsreadOnlyordisabledproperty istrue.wrapA string that represents the element's
wrapattribute, indicating how the control wraps text.
Instance methods
Also inherits methods from its parent interface,HTMLElement.
checkValidity()Returns
falseif the element is a candidate for constraint validation, and it does not satisfy its constraints. In this case, it also fires a cancelableinvalidevent at the control. It returnstrueif the control is not a candidate for constraint validation, or if it satisfies its constraints.reportValidity()This method reports the problems with the constraints on the element, if any, to the user. If there are problems, it fires a cancelable
invalidevent at the element, and returnsfalse; if there are no problems, it returnstrue.select()Selects the contents of the control.
setCustomValidity()Sets a custom validity message for the element. If this message is not the empty string, then the element is suffering from a custom validity error, and does not validate.
setRangeText()Replaces a range of text in the element with new text.
setSelectionRange()Selects a range of text in the element (but does not focus it).
Events
Also inherits events from its parent interface,HTMLElement.
Listen to these events usingaddEventListener() or by assigning an event listener to theoneventname property of this interface:
selecteventFires when some text has been selected.
selectionchangeeventFires when the text selection in a
<textarea>element has been changed.
Examples
>Autogrowing textarea example
Make a textarea autogrow while typing:
JavaScript
function autoGrow(field) { if (field.scrollHeight > field.clientHeight) { field.style.height = `${field.scrollHeight}px`; }}document.querySelector("textarea").addEventListener("keyup", (e) => { autoGrow(e.target);});CSS
textarea.no-scrollbars { overflow: hidden; width: 300px; height: 100px;}HTML
<form> <fieldset> <legend>Your comments</legend> <p><textarea></textarea></p> <p><input type="submit" value="Send" /></p> </fieldset></form>Insert HTML tags example
Insert some HTML tags in a textarea:
function insert(startTag, endTag) { const textArea = document.myForm.myTextArea; const start = textArea.selectionStart; const end = textArea.selectionEnd; const oldText = textArea.value; const prefix = oldText.substring(0, start); const inserted = startTag + oldText.substring(start, end) + endTag; const suffix = oldText.substring(end); textArea.value = `${prefix}${inserted}${suffix}`; const newStart = start + startTag.length; const newEnd = end + startTag.length; textArea.setSelectionRange(newStart, newEnd); textArea.focus();}function insertURL() { const newURL = prompt("Enter the full URL for the link"); if (newURL) { insert(`<a href="${newURL}">`, "</a>"); } else { document.myForm.myTextArea.focus(); }}const strong = document.querySelector("#format-strong");const em = document.querySelector("#format-em");const link = document.querySelector("#format-link");const code = document.querySelector("#format-code");strong.addEventListener("click", (e) => insert("<strong>", "</strong>"));em.addEventListener("click", (e) => insert("<em>", "</em>"));link.addEventListener("click", (e) => insertURL());code.addEventListener("click", (e) => insert("<code>", "</code>"));Decorate the span to behave like a link:
.intLink { cursor: pointer; text-decoration: underline; color: blue;}<form name="myForm"> <p> [ <span><strong>Bold</strong></span> | <span><em>Italic</em></span> | <span>URL</span> | <span>code</span> ] </p> <p> <textarea name="myTextArea" rows="10" cols="50">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut facilisis, arcu vitae adipiscing placerat, nisl lectus accumsan nisi, vitae iaculis sem neque vel lectus. Praesent tristique commodo lorem quis fringilla. Sed ac tellus eros. </textarea> </p></form>Specifications
| Specification |
|---|
| HTML> # htmltextareaelement> |