MDN self-hosted library · e01.org

HTMLTextAreaElement

HTML DOM

HTML DOM

The HTMLTextAreaElement interface provides properties and methods for manipulating the layout and presentation of textarea elements.

Instance properties

Also inherits properties from its parent interface, HTMLElement.

  • autocomplete
  • : A string that represents the element's autocomplete attribute.
  • cols
  • : A number that represents the element's cols attribute, indicating the visible width of the text area.
  • defaultValue
  • : A string that represents the control's default value, which behaves like the Node.textContent property.
  • dirName
  • : A string that represents the directionality of the element.
  • disabled
  • : A boolean that represents the element's disabled attribute, indicating that the control is not available for interaction.
  • form
  • : Returns a reference to the parent form element. If this element is not contained in a form element, it can be the id attribute of any form element in the same document or the value null.
  • labels
  • : Returns a NodeList of the label elements associated with this element.
  • maxLength
  • : A number that represents the element's maxlength attribute, indicating the maximum number of characters the user can enter. This constraint is evaluated only when the value changes.
  • minLength
  • : A number that represents the element's minlength attribute, indicating the minimum number of characters the user can enter. This constraint is evaluated only when the value changes.
  • name
  • : A string that represents the element's name attribute, containing the name of the control.
  • placeholder
  • : A string that represents the element's placeholder attribute, containing a hint to the user about what to enter in the control.
  • readOnly
  • : A boolean that represents the element's readonly attribute, indicating that the user cannot modify the value of the control.
  • required
  • : A boolean that represents the element's required attribute, indicating that the user must specify a value before submitting the form.
  • rows
  • : A number that represents the element's rows attribute, indicating the number of visible text lines for the control.
  • selectionDirection
  • : A string that represents the direction in which selection occurred. This is forward if selection was performed in the start-to-end direction of the current locale, or backward for the opposite direction. This can also be none if the direction is unknown.
  • selectionEnd
  • : A 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, and selectionStart as the first argument.
  • selectionStart
  • : A 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 and selectionEnd as the second argument.
  • textLength
  • : Returns the code point length of the control's value. Same as reading value.length.
  • type
  • : Returns the string textarea.
  • validationMessage
  • : Returns 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 (willValidate is false), or it satisfies its constraints.
  • validity
  • : Returns the validity state that this element is in.
  • value
  • : A string that represents the raw value contained in the control.
  • willValidate
  • : Returns whether the element is a candidate for constraint validation. false if any conditions bar it from constraint validation, including its readOnly or disabled property is true.
  • wrap
  • : A string that represents the element's wrap attribute, indicating how the control wraps text.

Instance methods

Also inherits methods from its parent interface, HTMLElement.

  • checkValidity()
  • : Returns false if the element is a candidate for constraint validation, and it does not satisfy its constraints. In this case, it also fires a cancelable invalid event at the control. It returns true if 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 invalid event at the element, and returns false; if there are no problems, it returns true.
  • 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 using addEventListener() or by assigning an event listener to the oneventname property of this interface:

  • select event
  • : Fires when some text has been selected.
  • selectionchange event
  • : Fires when the text selection in a textarea element has been changed.

Examples

Autogrowing textarea example

Make a textarea autogrow while typing:

HTML

<fieldset>
  <legend>Your comments</legend>
  <p><textarea class="no-scrollbars"></textarea></p>
</fieldset>

CSS

textarea.no-scrollbars {
  overflow: hidden;
  width: 300px;
  height: 100px;
}

JavaScript

function autoGrow(field) {
  if (field.scrollHeight > field.clientHeight) {
    field.style.height = `${field.scrollHeight}px`;
  }
}

document.querySelector("textarea").addEventListener("keyup", (e) => {
  autoGrow(e.target);
});

600

Insert HTML tags example

Insert some HTML tags in a textarea:

```js live-sample___insert-html const textArea = document.querySelector("#my-text-area");

function insert(startTag, endTag) { 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}">, ""); } else { textArea.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("", "")); em.addEventListener("click", (e) => insert("", "")); link.addEventListener("click", (e) => insertURL()); code.addEventListener("click", (e) => insert("", ""));


Decorate the span to behave like a link:

```css live-sample___insert-html
.intLink {
  cursor: pointer;
  text-decoration: underline;
  color: blue;
}

```html live-sample___insert-html

[ Bold | Italic | URL | code ]

```

Specifications

Browser compatibility