files/en-us/web/api/htmltextareaelement/index.md
{{APIRef("HTML DOM")}}
The HTMLTextAreaElement interface provides properties and methods for manipulating the layout and presentation of {{HTMLElement("textarea")}} elements.
{{InheritanceDiagram}}
Also inherits properties from its parent interface, {{DOMxRef("HTMLElement")}}.
autocomplete attribute.cols attribute, indicating the visible width of the text area.disabled attribute, indicating that the control is not available for interaction.id attribute of any {{HTMLElement("form")}} element in the same document or the value null.maxlength attribute, indicating the maximum number of characters the user can enter. This constraint is evaluated only when the value changes.minlength attribute, indicating the minimum number of characters the user can enter. This constraint is evaluated only when the value changes.name attribute, containing the name of the control.placeholder attribute, containing a hint to the user about what to enter in the control.readonly attribute, indicating that the user cannot modify the value of the control.required attribute, indicating that the user must specify a value before submitting the form.rows attribute, indicating the number of visible text lines for the control.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.setSelectionRange() had been called with this as the second argument, and selectionStart as the first argument.setSelectionRange() had been called with this as the first argument and selectionEnd as the second argument.value. Same as reading value.length.textarea.willValidate is false), or it satisfies its constraints.false if any conditions bar it from constraint validation, including its readOnly or disabled property is true.wrap attribute, indicating how the control wraps text.Also inherits methods from its parent interface, {{DOMxRef("HTMLElement")}}.
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.invalid event at the element, and returns false; if there are no problems, it returns true.Also inherits events from its parent interface, {{DOMxRef("HTMLElement")}}.
Listen to these events using {{domxref("EventTarget/addEventListener", "addEventListener()")}} or by assigning an event listener to the oneventname property of this interface:
Make a textarea autogrow while typing:
function autoGrow(field) {
if (field.scrollHeight > field.clientHeight) {
field.style.height = `${field.scrollHeight}px`;
}
}
document.querySelector("textarea").addEventListener("keyup", (e) => {
autoGrow(e.target);
});
textarea.no-scrollbars {
overflow: hidden;
width: 300px;
height: 100px;
}
<form>
<fieldset>
<legend>Your comments</legend>
<p><textarea class="no-scrollbars"></textarea></p>
<p><input type="submit" value="Send" /></p>
</fieldset>
</form>
{{EmbedLiveSample('Autogrowing_textarea_example', 600, 300)}}
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 class="intLink" id="format-strong"><strong>Bold</strong></span> |
<span class="intLink" id="format-em"><em>Italic</em></span> |
<span class="intLink" id="format-link">URL</span> |
<span class="intLink" id="format-code">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>
{{EmbedLiveSample('insert-html', , '300', , , , , 'allow-modals')}}
{{Specifications}}
{{Compat}}