Your Search Results


    « HTMLTextAreaElement

    The HTMLTextAreaElement interface, which provides special properties and methods (beyond the regular HTMLElement interface it also has available to it by inheritance) for manipulating the layout and presentation of <textarea> elements.


    Inherits properties from its parent, HTMLElement.

    Name Type Description
    accessKey DOMString Reflects the accesskey HTML attribute.
    autofocus Boolean Reflects the autofocus HTML attribute, indicating that the control should have input focus when the page loads
    cols unsigned long Reflects the cols HTML attribute, indicating the visible width of the text area.
    defaultValue DOMString The control's default value, which behaves like the element.textContent property.
    disabled Boolean Reflects the disabled HTML attribute, indicating that the control is not available for interaction.
    form Read only HTMLFormElement

    The containing form element, if this element is in a form. 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 Read only Unimplemented (see bug 556743) NodeList A list of <label> elements that are labels for this element.
    maxLength long Reflects the maxlength HTML attribute, indicating the maximum number of characters the user can enter. This constraint is evaluated only when the value changes.
    name DOMString Reflects name HTML attribute, containing the name of the control.
    placeholder DOMString Reflects the placeholder HTML attribute, containing a hint to the user about what to enter in the control.
    readOnly Boolean Reflects the readonly HTML attribute, indicating that the user cannot modify the value of the control.
    required Boolean Reflects the required HTML attribute, indicating that the user must specify a value before submitting the form.
    rows unsigned long Reflects the rows HTML attribute, indicating the number of visible text lines for the control.
    selectionDirection DOMString 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 unsigned long The index of the end of selected text. If no text is selected, 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 unsigned long The index of the beginning of selected text. If no text is selected, 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.
    tabIndex long The position of the element in the tabbing navigation order for the current document.
    textLength Read only long The codepoint length of the control's value.
    type Read only DOMString The string textarea.
    validationMessage Read only DOMString 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 Read only ValidityState The validity states that this element is in.
    value DOMString The raw value contained in the control.
    willValidate Boolean Indicates whether the element is a candidate for constraint validation. It is false if any conditions bar it from constraint validation.
    wrap DOMString Reflects the wrap HTML attribute, indicating how the control wraps text.

    The two properties tabIndex and accessKey are inherited from HTMLElement from HTML5 on, but were defined on HTMLTextAreaElement in DOM Level 2 HTML and earlier specifications.


    Inherits methods from its parent, HTMLElement.

    Name & Arguments Return Description
    blur() void Removes input focus from this control.
    checkValidity() Boolean Returns false if the button is a candidate for constraint validation, and it does not satisfy its constraints. In this case, it also fires an invalid event at the control. It returns true if the control is not a candidate for constraint validation, or if it satisfies its constraints.
    focus() void Gives input focus to this control.
    select() void Selects the contents of the control.
    setCustomValidity(in DOMString error) void 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.
    setSelectionRange(selectionStart, selectionEnd, [optional] selectionDirection) void Selects a range of text, and sets selectionStart and selectionEnd. If either argument is greater than the length of the value, it is treated as pointing to the end of the value. If end is less than start, then both are treated as the value of end.

    The two methods blur() and focus() are inherited from HTMLElement from HTML5 on, but were defined on HTMLTextAreaElement in DOM Level 2 HTML and earlier specifications.


    Autogrowing textarea example

    The following example shows how to make a textarea really autogrow while typing.

    Here is the JavaScript function:

    function autoGrow (oField) {
      if (oField.scrollHeight > oField.clientHeight) { = oField.scrollHeight + "px";


    textarea.noscrollbars {
      overflow: hidden;
      width: 300px;
      height: 100px;


    <form name="myForm">
        <legend>Your comments</legend>  
        <p><textarea class="noscrollbars" onkeyup="autoGrow(this);"></textarea></p>
        <p><input type="submit" value="Send" /></p>

    Insert HTML tags example

    The following example shows how to insert some HTML tags or smiles or any custom text in a textarea. First we create a JavaScript function:

    function insertMetachars(sStartTag, sEndTag) {
    	var bDouble = arguments.length > 1, oMsgInput = document.myForm.myTxtArea, nSelStart = oMsgInput.selectionStart, nSelEnd = oMsgInput.selectionEnd, sOldText = oMsgInput.value;
    	oMsgInput.value = sOldText.substring(0, nSelStart) + (bDouble ? sStartTag + sOldText.substring(nSelStart, nSelEnd) + sEndTag : sStartTag) + sOldText.substring(nSelEnd);
    	oMsgInput.setSelectionRange(bDouble || nSelStart === nSelEnd ? nSelStart + sStartTag.length : nSelStart, (bDouble ? nSelEnd : nSelStart) + sStartTag.length);

    Add a little bit of CSS to decorate the internal span to behave like a link:

    .intLink {
    	cursor: pointer;
    	text-decoration: underline;
    	color: #0000ff;

    And here is the HTML:

    <form name="myForm">
    <p>[&nbsp;<span class="intLink" onclick="insertMetachars('&lt;strong&gt;','&lt;\/strong&gt;');"><strong>Bold</strong></span> | <span class="intLink" onclick="insertMetachars('&lt;em&gt;','&lt;\/em&gt;');"><em>Italic</em></span> | <span class="intLink" onclick="var newURL=prompt('Enter the full URL for the link');if(newURL){insertMetachars('&lt;a href=\u0022'+newURL+'\u0022&gt;','&lt;\/a&gt;');}else{document.myForm.myTxtArea.focus();}">URL</span> | <span class="intLink" onclick="insertMetachars('\n&lt;code&gt;\n','\n&lt;\/code&gt;\n');">code</span> | <span class="intLink" onclick="insertMetachars(' :-)');">smile</span> | etc. etc.&nbsp;]</p>
    <p><textarea rows="10" cols="50" name="myTxtArea">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. Sed consectetur eleifend felis vitae luctus. Praesent sagittis, est eget bibendum tincidunt, ligula diam tincidunt augue, a fermentum odio velit eget mi. Phasellus mattis, elit id fringilla semper, orci magna cursus ligula, non venenatis lacus augue sit amet dui. Pellentesque lacinia odio id nisi pulvinar commodo tempus at odio. Ut consectetur eros porttitor nunc mollis ultrices. Aenean porttitor, purus sollicitudin viverra auctor, neque erat blandit sapien, sit amet tincidunt massa mi ac nibh. Proin nibh sem, bibendum ut placerat nec, cursus et lacus. Phasellus vel augue turpis. Nunc eu mauris eu leo blandit mollis interdum eget lorem. </textarea></p>

    Maximum length and number of lines example

    The following example shows how to create a textarea with a maximum number of characters per line and a maximum number of lines. First we create a function that takes the text field and a key event as input and determines if any of the limits have been reached. If the limit has not been reached, it will return the key.

    function checkRows(oField, oKeyEvent) {
    	var	nKey = (oKeyEvent || /* old IE */ window.event || /* check is not supported! */ { keyCode: 38 }).keyCode,
    		// put here the maximum number of characters per line:
    		nCols = 30,
    		// put here the maximum number of lines:
    		nRows = 5,
    		nSelS = oField.selectionStart, nSelE = oField.selectionEnd,
    		sVal = oField.value, nLen = sVal.length,
    		nBackward = nSelS >= nCols ? nSelS - nCols : 0,
    		nDeltaForw = sVal.substring(nBackward, nSelS).search(new RegExp("\\n(?!.{0," + String(nCols - 2) + "}\\n)")) + 1,
    		nRowStart = nBackward + nDeltaForw,
    		aReturns = (sVal.substring(0, nSelS) + sVal.substring(nSelE, sVal.length)).match(/\n/g),
    		nRowEnd = nSelE + nRowStart + nCols - nSelS,
    		sRow = sVal.substring(nRowStart, nSelS) + sVal.substring(nSelE, nRowEnd > nLen ? nLen : nRowEnd),
    		bKeepCols = nKey === 13 || nLen + 1 < nCols || /\n/.test(sRow) || ((nRowStart === 0 || nDeltaForw > 0 || nKey > 0) && (sRow.length < nCols || (nKey > 0 && (nLen === nRowEnd || sVal.charAt(nRowEnd) === "\n"))));
    	return (nKey !== 13 || (aReturns ? aReturns.length + 1 : 1) < nRows) && ((nKey > 32 && nKey < 41) || bKeepCols);

    In the HTML we just need to hook our function to the `onkeypress` event and specify that our textarea does not accept pasting:

    <p>Textarea with fixed number of characters per line:<br />
    <textarea cols="50" rows="10" name="myInput" onkeypress="return checkRows(this, event);" onpaste="return false;" /></textarea></p>


    Specification Status Comment
    WHATWG HTML Living Standard Living Standard The following attributes have been added: autocomplete, and inputmode.
    The following method has been added: setSelection().
    HTML5 Candidate Recommendation The attributes tabindex and accesskey, as well as the methods blur() and focus() are now defined on HTMLElement.
    The following attributes have been added: autofocus, placeholder, dirName, wrap, maxLength, required, textLength, labels, selectionStart, selectionEnd, selectionDirection, validity, validationMessage, and willValidate.
    The following methods have been added: checkValidity(), setCustomValidity(), and setSelectionRange().
    Document Object Model (DOM) Level 2 HTML Specification Recommendation The property defaultValue doesn't contain the initial value of the value attribute, but the initial value of the content of the <textarea>.
    Document Object Model (DOM) Level 1 Specification Recommendation Initial definition.


    Document Tags and Contributors

    Contributors to this page: Sheppy, kscarfone, jmchuma, jswisher, arasbm, fusionchess, teoli, Ms2ger, Ste Al Th
    Last updated by: teoli,